TEST(configure_tty, with_socat) { int socat_pid; int serial_fd; unsigned char rx_buff[2048]; pthread_t thread; socat_pid = start_socat(); serial_fd = configure_tty(tty_path); pthread_create(&thread, NULL, write_data_thread, &serial_fd); // read all packets while (received_packets < number_packets) { int n_chars = receive_data(serial_fd, rx_buff, 2048, handle_pkt); if (n_chars == -1) { // Happend when 'socat' didn't run correctly // Break to prevent infinite loop // // Main code does also quits when ret <= 0 fprintf(stderr, "ABORTING: %s\n", strerror(errno)); break; } } /* wait for writer thread */ while (pthread_tryjoin_np(thread, NULL)) sleep(1); ASSERT_EQ(number_packets, received_packets); kill(socat_pid, SIGINT); }
int main(int argc, char *argv[]) { int GPS_fd = 0; int M3_fd = 0; int line=0; int gotnewline=0; int fullline=0; int linelength=0; int gottime=0; int n_chars; int hours, minutes,seconds; uint32_t secondsSinceMidnight; unsigned char hours_string[2]; unsigned char minutes_string[2]; unsigned char seconds_string[2]; uint8_t rx_buff[256]; uint8_t tx_buff[256]; char *M3_path = "/dev/ttyA8_M3"; char *GPS_path= "/dev/ttyO1"; char rxchar; int ret; if ((GPS_fd = configure_tty(GPS_path,B9600)) <= 0) { printf("Could not open and configure TTY %s\n", GPS_path); close(GPS_fd); return -1; } if ((M3_fd = configure_tty(M3_path,B500000)) <= 0) { printf("Could not open and configure TTY %s\n", M3_path); close(M3_fd); return -1; } // // First we wait for a fresh NMEA time from the GPS // //discard partial lines (NMEA frames begins by '$') while (!gotnewline){ n_chars = read(GPS_fd, &rxchar, 1); if (n_chars=1 && rxchar=='$') { gotnewline=1; } } // read full new line (till next '$') // until we got a GPGGA one and its time while(!gottime){ while(!fullline){ n_chars = read(GPS_fd, &rxchar, 1); if (n_chars==1 && rxchar=='$') { fullline=1; rx_buff[linelength]=0; } else { if (n_chars==1) { rx_buff[linelength]=rxchar; linelength++; } } } linelength=0; fullline=0; //we look for a GPGGA NMEA frame which looks like : //$GPGGA,125315.00,4513.12989,N,00548.40890,E,1,06,1.47,230.0,M,47.4,M,,*5B // 125315.00 being the time of the day : 12h53mn15s if (strncmp(rx_buff,"GPGGA",5)==0) { sscanf(rx_buff,"GPGGA,%2s%2s%2s",&hours_string, &minutes_string, &seconds_string); hours=atoi(hours_string); minutes=atoi(minutes_string); seconds=atoi(seconds_string); secondsSinceMidnight=hours*3600+minutes*60+seconds; printf("Got %02d:%02d:%02d from GPS, sending %d to the M3\n",hours,minutes,seconds,secondsSinceMidnight); gottime=1; } } // // Then we send the time to the M3 // tx_buff[0]=MAGICCHAR1; tx_buff[1]=MAGICCHAR2; memcpy(tx_buff+2,&secondsSinceMidnight,4); if (write(M3_fd, tx_buff, 6)!=6) printf("Error writing to %s\n",GPS_path); printf("wrote %02x %02x %02x %02x\n",tx_buff[2],tx_buff[3],tx_buff[4],tx_buff[5]); close(M3_fd); close(GPS_fd); }
int main(int argc, char **argv) { int opt, rc = 0; while ((opt = getopt(argc, argv, "hv")) != -1) { switch (opt) { case 'v': conf.verbosity++; break; case 'h': default: return usage(argv[0]); } } conf.flx_ufd.fd = open(FLX_DEV, O_RDWR); if (conf.flx_ufd.fd < 0) { perror(FLX_DEV); rc = 1; goto finish; } if (!configure_tty(conf.flx_ufd.fd)) { fprintf(stderr, "%s: Failed to configure tty params\n", FLX_DEV); rc = 2; goto finish; } if (!config_init()) { rc = 3; goto oom; } if (!config_load_all()) { rc = 4; goto finish; } conf.ubus_ctx = ubus_connect(NULL); if (!conf.ubus_ctx) { fprintf(stderr, "Failed to connect to ubus\n"); rc = 5; goto finish; } #ifdef WITH_YKW conf.ykw = ykw_new(YKW_DEFAULT_THETA); if (conf.ykw == NULL) { rc = 6; goto oom; } #endif mosquitto_lib_init(); snprintf(conf.mqtt.id, MQTT_ID_LEN, MQTT_ID_TPL, getpid()); conf.mosq = mosquitto_new(conf.mqtt.id, conf.mqtt.clean_session, &conf); if (!conf.mosq) { switch (errno) { case ENOMEM: rc = 7; goto oom; case EINVAL: fprintf(stderr, "mosq_new: Invalid id and/or clean_session.\n"); rc = 8; goto finish; } } rc = mosquitto_loop_start(conf.mosq); switch (rc) { case MOSQ_ERR_INVAL: fprintf(stderr, "mosq_loop_start: Invalid input parameters.\n"); goto finish; case MOSQ_ERR_NOT_SUPPORTED: fprintf(stderr, "mosq_loop_start: No threading support.\n"); goto finish; }; rc = mosquitto_connect_async(conf.mosq, conf.mqtt.host, conf.mqtt.port, conf.mqtt.keepalive); switch (rc) { case MOSQ_ERR_INVAL: fprintf(stderr, "mosq_connect_async: Invalid input parameters.\n"); goto finish; case MOSQ_ERR_ERRNO: perror("mosq_connect_async"); goto finish; } uloop_init(); uloop_fd_add(&conf.flx_ufd, ULOOP_READ); uloop_timeout_set(&conf.timeout, CONFIG_ULOOP_TIMEOUT); ubus_add_uloop(conf.ubus_ctx); ubus_register_event_handler(conf.ubus_ctx, &conf.ubus_ev_sighup, CONFIG_UBUS_EV_SIGHUP); ubus_register_event_handler(conf.ubus_ctx, &conf.ubus_ev_shift_calc, CONFIG_UBUS_EV_SHIFT_CALC); uloop_run(); uloop_done(); goto finish; oom: fprintf(stderr, "error: Out of memory.\n"); finish: mosquitto_disconnect(conf.mosq); mosquitto_loop_stop(conf.mosq, false); mosquitto_destroy(conf.mosq); mosquitto_lib_cleanup(); ykw_free(conf.ykw); if (conf.ubus_ctx != NULL) { ubus_free(conf.ubus_ctx); } close(conf.flx_ufd.fd); uci_free_context(conf.uci_ctx); return rc; }
void wx::mbwxsave (char *taskname) //************************************************************************* // this task reads wx-data from serial line, saves it in intervals // of 5min and puts current + recent (-1h) wx-data into global // variable. //************************************************************************* { char name[20]; strcpy(name, "wx:mbwxsave"); time_t nextsavet = ad_time(); int fd; char rawdata[80]; long total_num = 0; // total number of measurements long comerr = 0; // total number of communication errors char lastcmd_s[50]; int orgok; lastcmd("init"); // configure tty device fd = configure_tty(m.wxtty); if (fd < 0) return; trace(replog, name, "WX logging started"); // initialize wx-structure memset(&m.wx, 0, sizeof(m.wx)); m.wx.data_valid = 0; // wait for data while (! runterfahren) { rawdata[0]=0; do { wdelay(50); while (! read_tty(fd, rawdata+strlen(rawdata), 79-strlen(rawdata))) { wdelay(50); // if no data received for >1min, old data is no longer valid if (m.wx.data_valid && ad_time()-m.wx.t > 600) { m.wx.data_valid = 0; lastcmd("no data"); trace(report, name, "no data received"); } } if (m.wxstnname[0] == '_') //simple hack to get debug output trace(report, "wx", "rawdata: \"%s\"", rawdata); //correct string starts with !!... if (strlen(rawdata) > 2 && rawdata[0] != '!') { char *a = strstr(rawdata, "!!"); if (a) memmove(rawdata, a, strlen(a)+1); else { rawdata[0]=0; comerr++; } } } // wait until line is complete (or buffer full) while(strlen(rawdata) < 70 && ! strstr(rawdata, "\n") && ! strstr(rawdata, "\r")); rm_crlf(rawdata); //calculate data, and if correct test if data should be saved orgok = orgdata(&m.wx, rawdata); if (orgok) comerr++; else total_num++; sprintf(lastcmd_s, "data %ld/%ld", total_num, comerr); lastcmd(lastcmd_s); if (orgok && nextsavet <= m.wx.t) { if (nextsavet < ad_time()) nextsavet = ad_time(); // if time is changed nextsavet += 300; //next time to save data is in 5min savedata(rawdata, &m.wx); } } close_tty(fd); }