aboutsummaryrefslogtreecommitdiffstats
path: root/src/emc/tooldata/tooldata_db.cc
blob: 4496ee2573f48f0b84cc2b1f3fa5342a77c084dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
** Copyright: 2021
** Author:    Dewey Garrett <dgarrett@panix.com>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include "tooldata.hh"
#include <sys/poll.h>

#define DB_VERSION "v2.1"
#define MAX_DB_PROGRAM_ARGS 10
#define UNEXPECTED_MSG fprintf(stderr,"UNEXPECTED %s %d\n",__FILE__,__LINE__);

static bool db_live  = 0;
static bool db_show  = 0; // use environmental var: DB_SHOW
static bool db_debug = 0; // use environmental var: DB_DEBUG

//-----------------------------------------------------------------
/* pipe code adapted from:
** https://jineshkj.wordpress.com/2006/12/22/how-to-capture-stdin-stdout-and-stderr-of-child-program/
*/

#define NUM_PIPES          2
// pipes[0][] parent -------> child
// pipes[1][] parent <------- child
// pipes[][0] is for read
// pipes[][1] is for write
#define PARENT_WRITE_PIPE  0
#define PARENT_READ_PIPE   1

static int pipes[NUM_PIPES][2];

#define READ_FD  0
#define WRITE_FD 1

#define PARENT_READ_FD  ( pipes[PARENT_READ_PIPE] [READ_FD]  )
#define PARENT_WRITE_FD ( pipes[PARENT_WRITE_PIPE][WRITE_FD] )

#define CHILD_READ_FD   ( pipes[PARENT_WRITE_PIPE][READ_FD]  )
#define CHILD_WRITE_FD  ( pipes[PARENT_READ_PIPE] [WRITE_FD] )

#define READ_TIMEOUT_MS 10000

static bool is_random_toolchanger = 0;
static char db_childname[PATH_MAX];

static void handle_sigchild(int s)
{
    pid_t pid;
    int status;
    while((pid = waitpid(-1, &status, WUNTRACED | WCONTINUED | WNOHANG)) > 0) ;
    if (WIFSIGNALED(status))  {
        db_live = 0;
        fprintf(stderr,"%5d !!!%s terminated by signal %d\n"
               ,getpid(),db_childname,WTERMSIG(status));
    }
    if (WIFEXITED(status)) {
        db_live = 0;
        fprintf(stderr,"%5d ===%s normal exit status=%d\n"
               ,getpid(),db_childname,WEXITSTATUS(status));
    }
#if 0
    if (WIFSTOPPED(status)) {
        fprintf(stderr,"%5d ===%s stopped\n",getpid(),db_childname);
    }
    if (WIFCONTINUED(status)) {
        fprintf(stderr,"%5d ===%s continued\n",getpid(),db_childname);
    }
#endif
}

static int fork_create(int myargc,char *const myargv[])
{
    // O_DIRECT:packet mode
    if (pipe2(pipes[PARENT_READ_PIPE],O_DIRECT)) {
        perror("E: pipe2");exit(EXIT_FAILURE);
    }
    if (pipe2(pipes[PARENT_WRITE_PIPE],0)) {
        perror("E: pipe2");exit(EXIT_FAILURE);
    }

    signal(SIGCHLD, handle_sigchild);
    pid_t childpid = fork();
    if (childpid == -1) {
         perror("E: fork failed");
         exit(EXIT_FAILURE);
    }

    if(!childpid) { // CHILD------------------------------------
        dup2(CHILD_READ_FD,  STDIN_FILENO);
        dup2(CHILD_WRITE_FD, STDOUT_FILENO);
        close(CHILD_READ_FD);   // not reqd by child
        close(CHILD_WRITE_FD);  // not reqd by child
        close(PARENT_READ_FD);  // not reqd by child
        close(PARENT_WRITE_FD); // not reqd by child
        if (execv(myargv[0], myargv)) {perror("execv fail");}
        return -1;
    } else { // PARENT------------------------------------------
        close(CHILD_READ_FD); //not reqd by parent
        close(CHILD_WRITE_FD);//not reqd by parent
        db_live = 1;
        return 0;
    }
    return -1;
} // fork_create()

static int readready (void)
{
    struct pollfd fds[1];
    fds[0].fd     = PARENT_READ_FD;
    fds[0].events = POLLIN;
    int fdct = poll(fds, 1, READ_TIMEOUT_MS);
    if (fdct == -1) { perror("poll"); return 0; }
    return fds[0].revents & POLLIN; // !=0 == true: ok
} // readready()

static int send_request(char *cmd)
{
    // expect request with \n
    if (db_debug) {fprintf(stderr,"SEND:  %s\n",cmd);}
    if (write(PARENT_WRITE_FD, cmd,strlen(cmd) ) < 0) {
        perror("send_request write");
        return -1;
    }
    return 0;
} // send_request()

static int read_reply(char reply[],int len)
{
    char buffer[CANON_TOOL_ENTRY_LEN];
    int count = 0;
    buffer[count] = 0;
    if (!db_live) {
        fprintf(stderr,"read_reply: db not active\n");
        return -1;
    }
    if ( !readready() ) {
        fprintf(stderr,"!!!! tooldata_db:read_reply fail: timeout_ms=%d\n"
               ,READ_TIMEOUT_MS);
        return -1;
    }
    count = read(PARENT_READ_FD, buffer, sizeof(buffer)-1);
    if (count > 1) {
        buffer[count] = 0;
        // strip \n
        if (buffer[strlen(buffer)-1] =='\n') {
            buffer[strlen(buffer)-1] = 0;
        }
    } else {
        fprintf(stderr,"read_reply IO Error count=%d\n",count);
        return -1;
    }
    snprintf(reply,len,"%s",buffer);
    if (strstr(reply,"NAK")) {
        fprintf(stderr,"!!!! %s\n",reply);
    }
    if (db_debug) {fprintf(stderr,"RECV:  %s\n",reply);}
    return strlen(reply);
} // read_reply()

static int send_and_verify(char* msg)
{
    if (send_request(msg)) { UNEXPECTED_MSG; return -1; }
    char reply[CANON_TOOL_ENTRY_LEN];
    if (read_reply(reply,sizeof(reply)) <0) {
        //fprintf(stderr,"%5d ??????replylen=%lu active=%d\n"
        //        ,getpid(),strlen(reply),db_live);
        return -1;
    }
    //fprintf(stderr,"REPLY: %s\n",reply);
    return 0;
} // send_and_verify()

int tooldata_db_getall() {
    if (!db_live) {return -1;}

    static bool initial_getall = 1;
    if (send_request((char*)"g\n")) {
        UNEXPECTED_MSG; return -1;
    }

    tooldata_reset();  // reset all idx

    if (is_random_toolchanger) {
        int ran_start_idx = 0;
        tooldata_add_init(ran_start_idx);
    } else {
        int nonran_start_idx = 1;
        tooldata_add_init(nonran_start_idx);
    }
    int ct=1;
    while (1) {
        char reply[CANON_TOOL_ENTRY_LEN];
        if (read_reply(reply,sizeof(reply)) <0) {
            //fprintf(stderr,"%5d ??????replylen=%lu active=%d\n"
            //        ,getpid(),strlen(reply),db_live);
            return -1;
        }
        if (initial_getall || db_show) {fprintf(stderr,"GET:   %s\n",reply);}
        if (strstr(reply,"FINI")) {
            //fprintf(stderr,"=====<ct=%3d>%s\n",ct,reply);
            break;
        }
        char **ttcomments = NULL; //not used with db

        int foundidx = tooldata_read_entry(reply,ttcomments);
        ct++;
        if (foundidx < 0) {
            fprintf(stderr,"!!!tooldata_db_getall %s\n",reply);
            return -1;
        }
    }
    // update the single-entry tbl file (typ: db_spindle.tbl)
    tooldata_save((char*)NULL,(char**)NULL);
    initial_getall = 0;
    return 0;
} // tooldata_db_getall()

int tooldata_db_init(char progname_plus_args[],int random_toolchanger)
{
    if (getenv( (char*)"DB_DEBUG") ) {db_debug = 1;}
    if (getenv( (char*)"DB_SHOW") )  {db_show  = 1;}
    int   child_argc = 0;
    char* child_argv[MAX_DB_PROGRAM_ARGS] = {0};
    char* token = strtok(progname_plus_args, " ");
    while (token != NULL) {
        child_argv[child_argc] = token;
        child_argc++;
        if (child_argc >= MAX_DB_PROGRAM_ARGS) {
            fprintf(stderr,"!!!!db_init: argc exceeds MAX_DB_PROGRAM_ARGS=%d\n"
                   ,MAX_DB_PROGRAM_ARGS);
            return -1;
        }
        token = strtok(NULL, " ");
    }
    snprintf(db_childname,sizeof(db_childname),"%s",child_argv[0]);
    is_random_toolchanger = random_toolchanger;

    if (access(child_argv[0],X_OK)) {
        fprintf(stderr,"!!!!db_init: <%s> not executable\n",child_argv[0]);
        return -1;
    }
    //fprintf(stderr,"=====db_childname=%s\n",db_childname);
    if (0 == fork_create(child_argc,child_argv) ) {
        //fprintf(stderr,"=====db_init forked %s\n",child_argv[0]);
    } else {
        fprintf(stderr,"!!!!!db_init: fork FAIL\n");
        perror("FORK");
        return -1;
    }

    // block for response
    fprintf(stderr,"====Waiting for %s version reply from %s\n",
            DB_VERSION,child_argv[0]);
    char reply[CANON_TOOL_ENTRY_LEN];

    if (read_reply(reply,sizeof(reply)) < 0 ) {
        fprintf(stderr,"!!!! tooldata_db_init: read_reply initial read failed\n");
        db_live = 0;
        return -1;
    } else {
        if (strncmp(reply,DB_VERSION,sizeof(DB_VERSION)) != 0) {
            fprintf(stderr,
                    "!!!! db_version MISMATCH require:%s received:%s\n",
                    DB_VERSION,reply);
            db_live = 0;
            return -1;
        } else {
            fprintf(stderr,"====Connected to %s\n",child_argv[0]);
        }
    }
    if (db_debug) {
        if (tooldata_db_getall()) {
            fprintf(stderr,"!!!! tooldata_db_init() get all fail\n");
            return -1;
        }
    }
    return 0;
} // tooldata_db_init()


int tooldata_db_notify(tool_notify_t ntype,
                       int toolno,
                       int pocketno,
                       CANON_TOOL_TABLE tdata)
{
    if (!db_live) return 0;   //silently ignore
    char **ttcomments = NULL; //not used with db
    char msg[CANON_TOOL_ENTRY_LEN +20];
    char buffer[CANON_TOOL_ENTRY_LEN] = {0};

    // make a tool line suitable for notifying db_program
    // caller may specify pocketno different from tdata.pocketno
    CANON_TOOL_TABLE notifydata;
    notifydata = tooldata_entry_init(); //nulls
    notifydata.toolno   = toolno;
    notifydata.pocketno = pocketno;
    switch (ntype) {
    case SPINDLE_LOAD: // 'l' command
         tooldata_format_toolline(pocketno,
                                  1, // ignore_zero_values
                                  notifydata, ttcomments, buffer);
         snprintf(msg,sizeof(msg),"l %s\n",buffer);
         if (db_debug) {fprintf(stderr,"SPINDLE_LOAD:%s\n",msg);}
         break;
    case SPINDLE_UNLOAD: // 'u' command
         tooldata_format_toolline(pocketno,
                                  1, // ignore_zero_values
                                  notifydata, ttcomments, buffer);
         snprintf(msg,sizeof(msg),"u %s\n",buffer);
         if (db_debug) {fprintf(stderr,"SPINDLE_UNLOAD:%s\n",msg);}
         break;
    case TOOL_OFFSET: // 'p' command
         notifydata          = tdata;  // from caller
         notifydata.toolno   = toolno;
         notifydata.pocketno = pocketno;
         tooldata_format_toolline(pocketno,
                                  0, // do not ignore_zero_values
                                  notifydata, ttcomments, buffer);
         snprintf(msg,sizeof(msg),"p %s\n",buffer);
         if (db_debug) {fprintf(stderr,"PUT:   %s\n",msg);}
         break;
    default: UNEXPECTED_MSG;
    }
    send_and_verify(msg);

    return 0;
} // tooldata_db_notify()

#undef UNEXPECTED_MSG
bues.ch cgit interface