void ICACHE_FLASH_ATTR pando_subdevice_recv(uint8_t * buffer, uint16_t length) { if(NULL == buffer) { return; } PRINTF("subdevive receive a package: \n"); show_package(buffer, length); struct sub_device_buffer *device_buffer = (struct sub_device_buffer *)pd_malloc(sizeof(struct sub_device_buffer)); device_buffer->buffer_length = length; device_buffer->buffer = (uint8 *)pd_malloc(length); pd_memcpy(device_buffer->buffer, buffer, length); uint16 payload_type = get_sub_device_payloadtype(device_buffer); switch (payload_type) { case PAYLOAD_TYPE_DATA: decode_data(device_buffer); break; case PAYLOAD_TYPE_COMMAND: decode_command(device_buffer); break; default: PRINTF("unsuported paload type : %d", payload_type); break; } delete_device_package(device_buffer); }
void CIRDriver::DaemonThreadProc(void) { /* Accept client connections, */ /* and watch the data buffer. */ /* When data comes in, decode it */ /* and send the result to clients. */ unsigned long data; char *message; Cwinlirc *app = (Cwinlirc *)AfxGetApp(); for(;;) { if(GetData(&data)==false) data=readdata(0,DaemonThreadEvent); use_ir_hardware=true; message=decode_command(data); if(message!=NULL) { //DEBUG("decode_command successful\n"); drvdlg->GoGreen(); app->server->send(message); } else { //DEBUG("decode_command failed\n"); } } }
int main (int argc, char *argv[]) { int i; ProgEnv env; int ret; env.path = "."; i = decode_switch (argc, argv, &env); if (i == argc) usage (argv[0], EXIT_FAILURE); env.trie_name = argv[i++]; env.sb_trie = sb_trie_open (env.path, env.trie_name, TRIE_IO_READ | TRIE_IO_WRITE | TRIE_IO_CREATE); if (!env.sb_trie) { fprintf (stderr, "Cannot open trie '%s' at '%s'\n", env.trie_name, env.path); exit (EXIT_FAILURE); } ret = decode_command (argc - i, argv + i, &env); sb_trie_close (env.sb_trie); return ret; }
void can_check_general(void) { uint8_t i = 0; uint8_t mb = 0; for(mb = 0; mb < 4; mb ++) { set_up_msg(mb); // Sets up the message object. if(can_get_status(&message, mob_number) != CAN_STATUS_NOT_COMPLETED) // wait for a message to come in. { if(message.status == MOB_RX_COMPLETED) { for (i = 0; i < 8; i ++) // Transfer the message to the receive array. { receive_arr[i] = *(message.pt_data + i); } switch(receive_arr[6]) // BIG TYPE { case MT_COM : decode_command(&receive_arr[0]); // SMALL TYPE break; case MT_HK : break; case MT_DATA : break; case MT_TC : break; default: break; } for (i = 0; i < 8; i ++) { receive_arr[i] = 0; // Reset the message array to zero after each message. } } clean_up_msg(mb); // Clean up the message object. } } can_check_housekeep(); return; }
int main() { init(); while(1) { switch(state) { case STATE_GET_COMMAND: UARTprintf(">"); get_command(); state = decode_command(); break; case STATE_PRINT_INFO: state = print_info(); break; case STATE_SET_MODE: state = set_mode(); break; case STATE_SET_INTERVAL: state = set_interval(); break; case STATE_SET_TRIGGER: state = set_trigger(); break; case STATE_RUN: state = run(); break; case STATE_STOP: state = stop(); break; case STATE_GET_BUFFER: state = get_buffer(); break; case STATE_GET_PARTIAL_BUFFER: state = get_partial_buffer(); break; case STATE_PRINT_STATE: state = print_state(); break; case STATE_LED: led_mask = led_mask ? 0 : 0xffffffff; state = STATE_GET_COMMAND; break; case STATE_RESET: reset(); state = STATE_GET_COMMAND; break; case STATE_UNKNOWN_COMMAND: UARTprintf("UNKNOWN COMMAND\r\n"); state = STATE_GET_COMMAND; break; } } return 0; }
void er59256_set_iobits(device_t *device, UINT8 newbits) { er59256_t *er59256 = get_token(device); //UINT32 bit; // Make sure we only apply valid bits newbits&=ALL_MASK; if(LOGLEVEL>1) { logerror("er59256:newbits=%02X : ",newbits); LOG_BITS(newbits); logerror(" io_bits=%02X : ",er59256->io_bits); LOG_BITS(er59256->io_bits); logerror(" old_io_bits=%02X : ",er59256->old_io_bits); LOG_BITS(er59256->old_io_bits); logerror(" bitcount=%d, in_shifter=%04X, out_shifter=%05X, flags=%02X\n",er59256->bitcount,er59256->in_shifter,er59256->out_shifter,er59256->flags); } // Only do anything if the inputs have changed if((newbits&IN_MASK)!=(er59256->io_bits&IN_MASK)) { // save the current state, then set the new one, remembering to preserve data out er59256->old_io_bits=er59256->io_bits; er59256->io_bits=(newbits & ~DO_MASK) | (er59256->old_io_bits&DO_MASK); if(CS_RISE(er59256)) { er59256->flags&=~FLAG_START_BIT; er59256->command=CMD_INVALID; } if(LOGLEVEL>1) { if(CK_RISE(er59256)) logerror("er59256:CK rise\n"); if(CS_RISE(er59256)) logerror("er59256:CS rise\n"); if(CK_FALL(er59256)) logerror("er59256:CK fall\n"); if(CS_FALL(er59256)) logerror("er59256:CS fall\n"); } if(CK_RISE(er59256) && CS_VALID(er59256)) { if((STARTED(er59256)==0) && (GET_DI(er59256)==1)) { er59256->bitcount=0; er59256->flags|=FLAG_START_BIT; } else { SHIFT_IN(er59256); er59256->bitcount++; if(er59256->bitcount==CMD_BITLEN) decode_command(er59256); if((er59256->bitcount==WRITE_BITLEN) && ((er59256->command & CMD_MASK)==CMD_WRITE)) { er59256->eerom[er59256->command & ADDR_MASK]=er59256->in_shifter; LOG(1,"er59256:write[%02X]=%04X\n",(er59256->command & ADDR_MASK),er59256->in_shifter); er59256->command=CMD_INVALID; } LOG(1,"out_shifter=%05X, io_bits=%02X\n",er59256->out_shifter,er59256->io_bits); SHIFT_OUT(er59256); } LOG(2,"io_bits:out=%02X\n",er59256->io_bits); } } }
void state_isotp() { int i, items, ret; char rxmsg[MAXLEN]; /* can to inet */ char buf[MAXLEN]; /* inet commands to can */ unsigned char isobuf[ISOTPLEN+1]; /* binary buffer for isotp socket */ unsigned char tmp; if(previous_state == STATE_ISOTP) { state_isotp_init(); previous_state = STATE_ISOTP; } FD_ZERO(&readfds); FD_SET(si, &readfds); FD_SET(client_socket, &readfds); /* * Check if there are more elements in the element buffer before calling select() and * blocking for new packets. */ if(more_elements) { FD_CLR(si, &readfds); } else { ret = select((si > client_socket)?si+1:client_socket+1, &readfds, NULL, NULL, NULL); if(ret < 0) { PRINT_ERROR("Error in select()\n") change_state(STATE_SHUTDOWN); return; } } if (FD_ISSET(si, &readfds)) { struct timeval tv = {0}; items = read(si, isobuf, ISOTPLEN); /* read timestamp data */ if(ioctl(si, SIOCGSTAMP, &tv) < 0) { PRINT_ERROR("Could not receive timestamp\n"); } if (items > 0 && items <= ISOTPLEN) { int startlen; sprintf(rxmsg, "< pdu %ld.%06ld ", tv.tv_sec, tv.tv_usec); startlen = strlen(rxmsg); for (i=0; i < items; i++) sprintf(rxmsg + startlen + 2*i, "%02X", isobuf[i]); sprintf(rxmsg + strlen(rxmsg), " >"); send(client_socket, rxmsg, strlen(rxmsg), 0); } } if (FD_ISSET(client_socket, &readfds)) { ret = receive_command(client_socket, buf); if(ret != 0) { change_state(STATE_SHUTDOWN); return; } if ( (ret = state_changed(buf, state)) ) { if(ret == CONTROL_SWITCH_STATE) state_isotp_deinit(); strcpy(buf, "< ok >"); send(client_socket, buf, strlen(buf), 0); return; } #if 0 if(!strcmp("< echo >", buf)) { send(client_socket, buf, strlen(buf), 0); return; } if(!strncmp("< sendpdu ", buf, 10)) { items = element_length(buf, 2); if (items & 1) { PRINT_ERROR("odd number of ASCII Hex values\n"); return; } items /= 2; if (items > ISOTPLEN) { PRINT_ERROR("PDU too long\n"); return; } for (i = 0; i < items; i++) { tmp = asc2nibble(buf[(2*i) + 10]); if (tmp > 0x0F) return; isobuf[i] = (tmp << 4); tmp = asc2nibble(buf[(2*i) + 11]); if (tmp > 0x0F) return; isobuf[i] |= tmp; } ret = write(si, isobuf, items); if(ret != items) { PRINT_ERROR("Error in write()\n") change_state(STATE_SHUTDOWN); return; } } else { PRINT_ERROR("unknown command '%s'.\n", buf) strcpy(buf, "< error unknown command >"); send(client_socket, buf, strlen(buf), 0); } #else ret = decode_command(buf); switch(ret) { case COMMAND_ECHO: send(client_socket, buf, strlen(buf), 0); break; case COMMAND_SENDPDU: items = element_length(buf, 2); if (items & 1) { PRINT_ERROR("odd number of ASCII Hex values\n"); return; } items /= 2; if (items > ISOTPLEN) { PRINT_ERROR("PDU too long\n"); return; } for (i = 0; i < items; i++) { tmp = asc2nibble(buf[(2*i) + 10]); if (tmp > 0x0F) return; isobuf[i] = (tmp << 4); tmp = asc2nibble(buf[(2*i) + 11]); if (tmp > 0x0F) return; isobuf[i] |= tmp; } ret = write(si, isobuf, items); if(ret != items) { PRINT_ERROR("Error in write()\n") change_state(STATE_SHUTDOWN); return; } break; /* COMMAND_SENDPDU */ case COMMAND_UNKNOWN: default: PRINT_ERROR("unknown command '%s'.\n", buf) strcpy(buf, "< error unknown command >"); send(client_socket, buf, strlen(buf), 0); break; /* COMMAND_UNKNOWN */ } #endif } }
void state_isotp_init() { int items, ret; struct sockaddr_can addr; struct ifreq ifr; static struct can_isotp_options opts; static struct can_isotp_fc_options fcopts; char buf[MAXLEN]; /* inet commands to can */ while(previous_state != STATE_ISOTP) { ret = receive_command(client_socket, buf); if(ret != 0) { change_state(STATE_SHUTDOWN); return; } strncpy(ifr.ifr_name, bus_name, IFNAMSIZ); if ( (ret = state_changed(buf, state)) ) { /* ensure proper handling in other states */ if(ret == CONTROL_SWITCH_STATE) previous_state = STATE_ISOTP; strcpy(buf, "< ok >"); send(client_socket, buf, strlen(buf), 0); return; } #if 0 if(!strcmp("< echo >", buf)) { send(client_socket, buf, strlen(buf), 0); continue; } memset(&opts, 0, sizeof(opts)); memset(&fcopts, 0, sizeof(fcopts)); memset(&addr, 0, sizeof(addr)); /* get configuration to open the socket */ if(!strncmp("< isotpconf ", buf, 12)) { items = sscanf(buf, "< %*s %x %x %x " "%hhu %hhx %hhu " "%hhx %hhx %hhx %hhx >", &addr.can_addr.tp.tx_id, &addr.can_addr.tp.rx_id, &opts.flags, &fcopts.bs, &fcopts.stmin, &fcopts.wftmax, &opts.txpad_content, &opts.rxpad_content, &opts.ext_address, &opts.rx_ext_address); /* < isotpconf XXXXXXXX ... > check for extended identifier */ if(element_length(buf, 2) == 8) addr.can_addr.tp.tx_id |= CAN_EFF_FLAG; if(element_length(buf, 3) == 8) addr.can_addr.tp.rx_id |= CAN_EFF_FLAG; if (((opts.flags & CAN_ISOTP_RX_EXT_ADDR) && items < 10) || ((opts.flags & CAN_ISOTP_EXTEND_ADDR) && items < 9) || ((opts.flags & CAN_ISOTP_RX_PADDING) && items < 8) || ((opts.flags & CAN_ISOTP_TX_PADDING) && items < 7) || (items < 5)) { PRINT_ERROR("Syntax error in isotpconf command\n"); /* try it once more */ continue; } /* open ISOTP socket */ if ((si = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) { PRINT_ERROR("Error while opening ISOTP socket %s\n", strerror(errno)); /* ensure proper handling in other states */ previous_state = STATE_ISOTP; state_isotp_deinit(); change_state(STATE_SHUTDOWN); return; } strcpy(ifr.ifr_name, bus_name); if(ioctl(si, SIOCGIFINDEX, &ifr) < 0) { PRINT_ERROR("Error while searching for bus %s\n", strerror(errno)); /* ensure proper handling in other states */ previous_state = STATE_ISOTP; state_isotp_deinit(); change_state(STATE_SHUTDOWN); return; } addr.can_family = PF_CAN; addr.can_ifindex = ifr.ifr_ifindex; /* only change the built-in defaults when required */ if (opts.flags) setsockopt(si, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts)); setsockopt(si, SOL_CAN_ISOTP, CAN_ISOTP_RECV_FC, &fcopts, sizeof(fcopts)); PRINT_VERBOSE("binding ISOTP socket...\n") if (bind(si, (struct sockaddr *)&addr, sizeof(addr)) < 0) { PRINT_ERROR("Error while binding ISOTP socket %s\n", strerror(errno)); /* ensure proper handling in other states */ previous_state = STATE_ISOTP; state_isotp_deinit(); change_state(STATE_SHUTDOWN); return; } /* ok we made it and have a proper isotp socket open */ previous_state = STATE_ISOTP; } #else ret = decode_command(buf); switch(ret) { case COMMAND_ECHO: send(client_socket, buf, strlen(buf), 0); continue; break; case COMMAND_ISOTPCONF: memset(&opts, 0, sizeof(opts)); memset(&fcopts, 0, sizeof(fcopts)); memset(&addr, 0, sizeof(addr)); /* get configuration to open the socket */ items = sscanf(buf, "< %*s %x %x %x " "%hhu %hhx %hhu " "%hhx %hhx %hhx %hhx >", &addr.can_addr.tp.tx_id, &addr.can_addr.tp.rx_id, &opts.flags, &fcopts.bs, &fcopts.stmin, &fcopts.wftmax, &opts.txpad_content, &opts.rxpad_content, &opts.ext_address, &opts.rx_ext_address); /* < isotpconf XXXXXXXX ... > check for extended identifier */ if(element_length(buf, 2) == 8) addr.can_addr.tp.tx_id |= CAN_EFF_FLAG; if(element_length(buf, 3) == 8) addr.can_addr.tp.rx_id |= CAN_EFF_FLAG; if (((opts.flags & CAN_ISOTP_RX_EXT_ADDR) && items < 10) || ((opts.flags & CAN_ISOTP_EXTEND_ADDR) && items < 9) || ((opts.flags & CAN_ISOTP_RX_PADDING) && items < 8) || ((opts.flags & CAN_ISOTP_TX_PADDING) && items < 7) || (items < 5)) { PRINT_ERROR("Syntax error in isotpconf command\n"); /* try it once more */ continue; } /* open ISOTP socket */ if ((si = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) { PRINT_ERROR("Error while opening ISOTP socket %s\n", strerror(errno)); /* ensure proper handling in other states */ previous_state = STATE_ISOTP; state_isotp_deinit(); change_state(STATE_SHUTDOWN); return; } strcpy(ifr.ifr_name, bus_name); if(ioctl(si, SIOCGIFINDEX, &ifr) < 0) { PRINT_ERROR("Error while searching for bus %s\n", strerror(errno)); /* ensure proper handling in other states */ previous_state = STATE_ISOTP; state_isotp_deinit(); change_state(STATE_SHUTDOWN); return; } addr.can_family = PF_CAN; addr.can_ifindex = ifr.ifr_ifindex; /* only change the built-in defaults when required */ if (opts.flags) setsockopt(si, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts)); setsockopt(si, SOL_CAN_ISOTP, CAN_ISOTP_RECV_FC, &fcopts, sizeof(fcopts)); PRINT_VERBOSE("binding ISOTP socket...\n") if (bind(si, (struct sockaddr *)&addr, sizeof(addr)) < 0) { PRINT_ERROR("Error while binding ISOTP socket %s\n", strerror(errno)); /* ensure proper handling in other states */ previous_state = STATE_ISOTP; state_isotp_deinit(); change_state(STATE_SHUTDOWN); return; } /* ok we made it and have a proper isotp socket open */ previous_state = STATE_ISOTP; break; case COMMAND_UNKNOWN: default: break; } /* switch */ #endif } /* while */
int main(int argc, char** argv) { int rc = 0; struct pando_buffer *bin_buf; uint16_t payload_type = 0; MQTTMessage msg; unsigned char buf[500]; unsigned char readbuf[500]; char payload_type_c[] = {'c', 'e', 'd'}; bin_buf = construct_data_package_to_server(&payload_type); decode_data(bin_buf, PAYLOAD_TYPE_DATA); /* command test, event is same as command except the type */ bin_buf = constuct_event_package_to_server(&payload_type); decode_command(bin_buf, PAYLOAD_TYPE_COMMAND); return 0; if (argc < 2) usage(); char* topic = argv[1]; getopts(argc, argv); if (strchr(topic, '#') || strchr(topic, '+')) opts.showtopics = 1; if (opts.showtopics) printf("topic is %s\n", topic); Network n; Client c; signal(SIGINT, cfinish); signal(SIGTERM, cfinish); NewNetwork(&n); ConnectNetwork(&n, opts.host, opts.port); MQTTClient(&c, &n, 1000, buf, 100, readbuf, 100); MQTTPacket_connectData data = MQTTPacket_connectData_initializer; data.willFlag = 0; data.MQTTVersion = 3; data.clientID.cstring = opts.clientid; data.username.cstring = opts.username; data.password.cstring = opts.password; data.keepAliveInterval = 10; data.cleansession = 1; printf("Connecting to %s %d\n", opts.host, opts.port); rc = MQTTConnect(&c, &data); printf("Connected %d\n", rc); printf("Subscribing to %s\n", topic); rc = MQTTSubscribe(&c, topic, opts.qos, messageArrived); printf("Subscribed %d\n", rc); while (!toStop) { MQTTYield(&c, 1000); if (toSend) { if (strstr(topic, "/d")) { /* data test */ bin_buf = construct_data_package_to_server(&payload_type); //decode_data(bin_buf, PAYLOAD_TYPE_DATA); } if (strstr(topic, "/e")) { /* command test */ bin_buf = constuct_event_package_to_server(&payload_type); //decode_command(bin_buf, PAYLOAD_TYPE_COMMAND); } msg.qos = opts.qos; msg.retained = 0; msg.dup = 0; msg.payload = bin_buf->buffer + bin_buf->offset; msg.payloadlen = bin_buf->buff_len - bin_buf->offset; /* before publish a message, you need to generate a topic with payload_type max device id is 8 bytes, hex to char need 8*2 bytes, '/' need 1 byte, type need 1 byte */ char publish_topic[8*2 + 1 + 1]; memset(publish_topic, 0, sizeof(publish_topic)); sprintf(publish_topic, "%s/%c", opts.clientid, payload_type_c[payload_type - 1]); printf(publish_topic); rc = MQTTPublish(&c, publish_topic, &msg); toSend = 0; pd_free(bin_buf); printf("published rc %d, len %d\n", rc, (int)msg.payloadlen); } } printf("Stopping\n"); MQTTDisconnect(&c); n.disconnect(&n); return 0; }