/* Get the current Module ID and see if it has changed since we last checked and updated things. If it has changed, get numerous values from the controller and send to vnmrj to get it up to date. If the probe is unplugged then the same probe is plugged in again, all params will be set to defaults including a set point of 0. I will check for a change in set point from non-zero to zero also. checkModuleChange() MUST ONLY BE CALLED FROM WITHIN MASSpeed(), else it can be trying to send cmds to the controller at the same time MASSpeed() is sending cmds, and the return values get out of sync. */ void checkModuleChange() { char strRes[80]; int status; int curSetPoint; sendToMASSpeed("MODULE\n"); status = getMASSpeedResponseString(strRes); if(status == 1) { if(strcmp(strRes, module) != 0) { // New module name found, update the one we keep around strcpy(module, strRes); DPRINT1(1,"Found new Module %s\n", module); // Need to get all new params getAllParams(); // No need to check set point below, our work is done. return; } } else { // Try clearing out controller and hope for better // luck next time. ckMASSpeedInternal(); } // The module did not change. However, if they unplugged and replugged, // the connector, the controller will reset itself. Try looking at // the set point also if it was non-zero and see if it has changed // to zero. if(setPoint != 0) { sendToMASSpeed("SETP\n"); status = getMASSpeedResponseInt(&curSetPoint); if(status == 1) { if(setPoint != curSetPoint) { setPoint = curSetPoint; // Need to get all new params getAllParams(); } } else { // Try clearing out controller and hope for better // luck next time. ckMASSpeedInternal(); } } }
void processMasMessage(char *msgBuf) { char strRes[80]; int len; int status; int response; /* For testing. when a msg is sent to the msgQ manually, it does not have the \n, so add one. */ len = strlen(msgBuf); if(msgBuf[len-1] != '\n') { strcat(msgBuf, "\n"); } DPRINT1(1, "Message received for MAS Controller: %s\n", msgBuf); // The msg should start with "SET" or "GET". The real difference // between these two, is that "SET" does not expect a return // value, and "GET" does expect a return. The command // GET BEARING is special in that it needs to return 3 values. // It is the only cmd to return the name of a value followed // by an equal sign and then a value. The general form of the // incoming message here should be "GET/SET CMD [value]" // Check first 3 char of msg for GET/SET if(strncmp(msgBuf, "SET", 3) == 0) { /* The controller return from all set commands should simply be the controller prompt of "\n>". Cmd strings received here (msgBuf) should be SET followed by the controller cmd string such as ACTSP which is followed by the value or values for that cmd. It should also already have a \n at the end of the string so that it is ready to send to the controller after removing the leading SET in the string. The msgBuf[4] is taking the string starting after the SET. */ sendToMASSpeed(&msgBuf[4]); // The controller should just return its prompt. status = getMASSpeedResponseString(strRes); if(status != 1 || strRes[1] != '>') { if(strncmp(strRes, "SPEED LIMIT", 11) == 0) { errLogRet(LOGIT,debugInfo, "Trying to set speed over speed limit\n"); } else { errLogRet(LOGIT,debugInfo, "Problem with controller sending cmd: %s\n %s\n", &msgBuf[4], strRes); // Try clearing out controller and hope for better // luck next time. ckMASSpeedInternal(); } } else { if(strncmp(msgBuf, "SET S", 5) == 0) { // If we set a new speed, ask the controller for // its current value to be sure it know what we ask for. sendToMASSpeed("SETP\n"); status = getMASSpeedResponseInt(&response); if(status == 1) { // Set the current value into the NDDS structure pCurrentStatBlock->AcqSpinSet = response; sendConsoleStatus(); // Save cur set point for test now and then setPoint = response; } } } } else if(strncmp(msgBuf, "GET", 3) == 0) { /* [4] means look at string after the "GET " in the command. */ if(strncmp(&msgBuf[4], "BEARING", 7) == 0) { /* BEARING command returns results in the form: SPAN = 100 ADJ = 6 MAX = 26\n> */ sendToMASSpeed("BEARING\n"); status = getMASSpeedResponseString(strRes); if(status != 1) { errLogRet(LOGIT,debugInfo, "Problem with Bearing command\n %s\n", strRes); // Try clearing out controller and hope for better // luck next time. ckMASSpeedInternal(); } } /* MODULE returns a text string */ else if(strncmp(&msgBuf[4], "MODULE", 6) == 0) { /* The module ID will not be sent unless it has changed. set to empty and then to the received value. */ pCurrentStatBlock->probeId1[0] = '\0'; sendConsoleStatus(); taskDelay(calcSysClkTicks(830)); /* 830 ms, or taskDelay(50); */ sendToMASSpeed(&msgBuf[4]); status = getMASSpeedResponseString(strRes); if(status != 1) { errLogRet(LOGIT,debugInfo, "Problem with command: %s\n %s\n", &msgBuf[4], strRes); // Try clearing out controller and hope for better // luck next time. ckMASSpeedInternal(); } // Put the result in place to be sent up strcpy(pCurrentStatBlock->probeId1, strRes); // Tell it we have changed values sendConsoleStatus(); // Save current module strcpy(module, strRes); DPRINT1(1, "MAS Probe Module: %s\n", module); } /* PROFILE returns a text string */ else if(strncmp(&msgBuf[4], "PROFILE", 7) == 0) { sendToMASSpeed(&msgBuf[4]); status = getMASSpeedResponseString(strRes); if(status != 1) { errLogRet(LOGIT, debugInfo, "Problem with command: %s\n %s\n", &msgBuf[4], strRes); // Try clearing out controller and hope for better // luck next time. ckMASSpeedInternal(); } } /* SETP returns the current set point value in Hz. Set with S */ else if(strncmp(&msgBuf[4], "SETP", 4) == 0) { sendToMASSpeed(&msgBuf[4]); status = getMASSpeedResponseInt(&response); if(status == 1) { // Set the current value into the NDDS structure pCurrentStatBlock->AcqSpinSet = response; sendConsoleStatus(); // Save cur set point for test now and then setPoint = response; } else { errLogRet(LOGIT,debugInfo, "Problem with command: %s\n %s\n", &msgBuf[4], strRes); // Try clearing out controller and hope for better // luck next time. ckMASSpeedInternal(); } } /* ACTSP returns 0, 1 or 2. Just forward the value. This is the active setpoint in case the user is wanting to change between set points without changing SETP */ else if(strncmp(&msgBuf[4], "ACTSP", 5) == 0) { sendToMASSpeed(&msgBuf[4]); status = getMASSpeedResponseString(strRes); if(status != 1) { errLogRet(LOGIT,debugInfo, "Problem with command: %s\n %s\n", &msgBuf[4], strRes); // Try clearing out controller and hope for better // luck next time. ckMASSpeedInternal(); } } /* SPEED LIMIT returns the current value as 'MAX SPEED = value' */ else if(strncmp(&msgBuf[4], "SPEED LIMIT", 11) == 0) { sendToMASSpeed(&msgBuf[4]); status = getMASSpeedResponseString(strRes); if(status != 1) { errLogRet(LOGIT,debugInfo, "Problem with command: %s\n %s\n", &msgBuf[4], strRes); // Try clearing out controller and hope for better // luck next time. ckMASSpeedInternal(); } } /* S stands for speed. This gets the current speed in Hz. This test must be after all other S commands so that we do not catch the first letter of another command. */ else if(strncmp(&msgBuf[4], "S", 1) == 0) { sendToMASSpeed(&msgBuf[4]); status = getMASSpeedResponseString(strRes); if(status != 1) { errLogRet(LOGIT, debugInfo, "Problem with command: %s\n %s\n", &msgBuf[4], strRes); // Try clearing out controller and hope for better // luck next time. ckMASSpeedInternal(); } } else if(strncmp(&msgBuf[4], "ALLPARAMS", 9) == 0) { getAllParams(); } else { // Error errLogRet(LOGIT, debugInfo, "Unknown speed controller command %s. Skipping.\n", msgBuf); } } else { // Error errLogRet(LOGIT, debugInfo, "Problem with speed controller command %s. Skipping.\n", msgBuf); } }
/* Task to be spawned by startMASSpeed. This task it to use a msgQ for a dual purpose. #1 is to have waiting on the msgQ time out every few seconds so we can get the current speed and put it in the NDDS structure for sending to vnmrj. #2 is that any request coming from vnmrj will be put into the msgQ and promptly dealt with. Thus, the same task sitting on a single msgQ will be waiting for requests from vnmrj and basically looping to get the current speed and send back to vnmrj. */ void MASSpeed() { int response; int status, mStatus; char msgBuf[MAX_MSG_LEN]; long cycle=0; int delay; int len; DPRINT(1, "*** Starting MASSpeed in Sleep State\n"); // Start up in sleep state. It will be awaken when spintype is set MasSpeedTaskSleeping = TRUE; msgToMASSpeedCtl = msgQCreate(MAX_MSGS, MAX_MSG_LEN, MSG_Q_FIFO); checkModuleChange(); // Get all param values and set into pCurrentStatBlock getAllParams(); // convert CYCLE_DELAY in sec to clock ticks delay = clkRate * CYCLE_DELAY; while(1) { // Now and then, check to see if the probe module has changed. // If it has, get all info and send back to vnmrj. if(!MasSpeedTaskSleeping) { if(cycle >= 20) { checkModuleChange(); cycle = 0; } cycle++; } /* Either catch a message from vnmrj, or timeout and get the current speed and send it. */ mStatus = msgQReceive(msgToMASSpeedCtl, msgBuf, MAX_MSG_LEN, delay); // If sleeping, just continue for another cycle of delay until // we are not sleeping. Ignore all commands that come in. if(MasSpeedTaskSleeping) continue; /* Timeout of msgQReceive will return ERROR. In our case, this simply means to get the speed and send it on. */ if(mStatus == ERROR) { /* See if we need to exit this task */ if(exitMas) { DPRINT(0, "Exiting MASSpeed Task\n"); masSpeedTaskId = 0; close(masSpeedPort); return; } sendToMASSpeed("S\n"); status = getMASSpeedResponseInt(&response); if(status == -1 && response == -2) errLogRet(LOGIT,debugInfo, "*** Trying to set speed higher than limit.\n"); else if(status == -1) errLogRet(LOGIT,debugInfo, "*** Speed Controller had problem with 'S' command.\n"); else { // Set the new response into the NDDS structure pCurrentStatBlock->AcqSpinAct = response; // Tell it we have changed a value sendConsoleStatus(); DPRINT1(3, "MAS Current Speed = %d\n", response); // Save current and prev for setting LED prevMasSpeed = curMasSpeed; curMasSpeed = response; // Set the LED as appropriate checkSpinLEDMas (); } } /* If msgQReceive does not return ERROR, then we received a message which we need to process. */ else { processMasMessage(msgBuf); } } }
int main( int argc, char *argv[] ) { jobj = json_object_new_object(); char Age[3] = ""; char Key[3] = ""; char Sex[5] = ""; char Distance[10] = ""; sqlite3 *db; char *zErrMsg = 0; int rc; while( FCGI_Accept() >= 0 ) { printf( "Content-Type: text/plain\n\n" ); getAllParams(); getParam("age", Age); getParam("key", Key); getParam("sex", Sex); getParam("distace", Distance); rc = sqlite3_open("wma2010.db", &db); if( rc ){ printf("Can't open database: %s\n", sqlite3_errmsg(db)); sqlite3_close(db); } char sql [128]; char table [7]; if (Key[0]=='c') { strcpy(table,"_road"); } else { strcpy(table,"_track"); } sprintf(sql,"SELECT %s FROM %s%s WHERE age=%s",Key,Sex,table,Age); rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg); if( rc!=SQLITE_OK ){ printf("SQL error: %s\n", zErrMsg); } sprintf(sql,"SELECT * FROM %s_key WHERE keycode='%s'",Sex,Key); rc = sqlite3_exec(db, sql, callback_final, 0, &zErrMsg); if( rc!=SQLITE_OK ){ printf("SQL error: %s\n", zErrMsg); } sqlite3_close(db); } return 0; }