/* to tail */ void zlst_push(void *PHEAD, void *ZNODE) { struct znode *head; struct znode *znode; if(!ZNODE) { RPT(RPT_ERR, "push: bad node"); return; } znode = (struct znode *)ZNODE; if(!PHEAD) { RPT(RPT_ERR, "push: NOT a list"); return; } head = *(struct znode **)PHEAD; if(!head) { RPT(RPT_INF, "push: into empty list"); znode->tail = znode; znode->next = NULL; znode->prev = NULL; *(struct znode **)PHEAD = znode; return; } RPT(RPT_INF, "push: ok"); znode->next = NULL; znode->prev = head->tail; head->tail->next = znode; /* head->tail is ok! */ head->tail = znode; return; }
/* it's up to the caller to free the node! */ void *zlst_pop(void *PHEAD) { struct znode *head; struct znode *tail; if(!PHEAD) { RPT(RPT_ERR, "pop: NOT a list"); return NULL; } head = *(struct znode **)PHEAD; if(!head) { RPT(RPT_INF, "pop: from empty list"); return NULL; } tail = head->tail; if(!(head->next)) { RPT(RPT_INF, "pop: from one node list"); *(struct znode **)PHEAD = NULL; return head; } if(!(tail->prev)) { RPT(RPT_ERR, "pop: bad list"); return NULL; } RPT(RPT_INF, "pop: ok"); tail->prev->next = NULL; head->tail = tail->prev; return tail; }
/* it's up to the caller to free the node! */ void *zlst_shift(void *PHEAD) { struct znode *head; if(!PHEAD) { RPT(RPT_ERR, "shift: NOT a list"); return NULL; } head = *(struct znode **)PHEAD; if(!head) { RPT(RPT_INF, "shift: from empty list"); return NULL; } if(!(head->next)) { RPT(RPT_INF, "shift: from one node list"); *(struct znode **)PHEAD = NULL; return head; } RPT(RPT_INF, "shift: ok"); head->next->prev = NULL; head->next->tail = head->tail; *(struct znode **)PHEAD = head->next; return head; }
/* to head */ void zlst_unshift(void *PHEAD, void *ZNODE) { struct znode *head; struct znode *znode; if(!ZNODE) { RPT(RPT_ERR, "unshift: bad node"); return; } znode = (struct znode *)ZNODE; if(!PHEAD) { RPT(RPT_ERR, "unshift: NOT a list"); return; } head = *(struct znode **)PHEAD; if(!head) { RPT(RPT_INF, "unshift: into empty list"); znode->tail = znode; znode->next = NULL; znode->prev = NULL; *(struct znode **)PHEAD = znode; return; } RPT(RPT_INF, "unshift: ok"); znode->tail = head->tail; znode->next = head; znode->prev = NULL; head->prev = znode; *(struct znode **)PHEAD = znode; return; }
void *zlst_search(void *PHEAD, int key) { struct znode *head; struct znode *znode; if(!PHEAD) { RPT(RPT_ERR, "search: NOT a list"); return NULL; } head = *(struct znode **)PHEAD; if(!head) { RPT(RPT_INF, "search: in an empty list"); return NULL; } for(znode = head; znode; znode = znode->next) { if(znode->key == key) { RPT(RPT_INF, "search: got %d", key); return znode; } } RPT(RPT_INF, "search: no %d", key); return NULL; }
void zlst_set_name(void *ZNODE, const char *name) { struct znode *znode; if(!ZNODE) { RPT(RPT_ERR, "set name: bad node"); return; } znode = (struct znode *)ZNODE; RPT(RPT_INF, "set name: %s", name); znode->name = name; /* the string should be const */ return; }
void zlst_set_key(void *ZNODE, int key) { struct znode *znode; if(!ZNODE) { RPT(RPT_ERR, "set key: bad node"); return; } znode = (struct znode *)ZNODE; RPT(RPT_INF, "set key: %d", key); znode->key = key; return; }
size_t udp_read(intptr_t id, void *buf) { struct udp *udp = (struct udp *)id; if(NULL == udp) { RPT(RPT_ERR, "bad id"); return -1; } size_t rslt = 0; fd_set fds; FD_ZERO(&fds); FD_SET(udp->sock, &fds); if(select(udp->sock + 1, &fds, NULL, NULL, NULL) < 0) { report("select failed"); return 0; } if(FD_ISSET(udp->sock, &fds)) { rslt = recvfrom(udp->sock, buf, UDP_LENGTH_MAX, 0, (struct sockaddr *)&(udp->remote), &(udp->socklen)); } return rslt; }
int main(int argc, char *argv[]) { unsigned char bbuf[ 204 + 10]; /* bin data buffer */ char tbuf[1024 + 10]; /* txt data buffer */ if(0 != deal_with_parameter(argc, argv)) { return -1; } fd_i = url_open(file_i, "rb"); if(NULL == fd_i) { RPT(RPT_ERR, "open \"%s\" failed", file_i); return -1; } pkt_addr = 0; while(1 == url_read(bbuf, npline, 1, fd_i)) { fprintf(stdout, "*ts, "); b2t(tbuf, bbuf, 188); fprintf(stdout, "%s", tbuf); fprintf(stdout, "*addr, %llX, \n", pkt_addr); pkt_addr += npline; } url_close(fd_i); return 0; }
static int deal_with_parameter(int argc, char *argv[]) { int i; if(1 == argc) { /* no parameter */ fprintf(stderr, "No URL to process...\n\n"); show_help(); return -1; } for(i = 1; i < argc; i++) { if('-' == argv[i][0]) { if(0 == strcmp(argv[i], "-h") || 0 == strcmp(argv[i], "--help")) { show_help(); return -1; } else if(0 == strcmp(argv[i], "-v") || 0 == strcmp(argv[i], "--version")) { show_version(); return -1; } else { RPT(RPT_ERR, "wrong parameter: %s", argv[i]); return -1; } } else { strcpy(file_i, argv[i]); } } return 0; }
int udp_close(intptr_t id) { struct udp *udp = (struct udp *)id; if(NULL == udp) { RPT(RPT_ERR, "bad id"); return -1; } /* manage multicast */ if(IN_MULTICAST(ntohl(inet_addr(udp->addr)))) { if('\0' == udp->src_addr[0]) { struct ip_mreq imreq; RPT(RPT_INF, "IP_DROP_MEMBERSHIP: %s", udp->addr); imreq.imr_interface.s_addr = htonl(INADDR_ANY); imreq.imr_multiaddr.s_addr = inet_addr(udp->addr); if(setsockopt(udp->sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (char *)&imreq, sizeof(imreq)) != 0) { report("IP_DROP_MEMBERSHIP failed"); } } else { struct ip_mreq_source mreqsrc; RPT(RPT_INF, "IP_DROP_SOURCE_MEMBERSHIP: %s@%s", udp->src_addr, udp->addr); mreqsrc.imr_interface.s_addr = htonl(INADDR_ANY); mreqsrc.imr_multiaddr.s_addr = inet_addr(udp->addr); mreqsrc.imr_sourceaddr.s_addr = inet_addr(udp->src_addr); if(setsockopt(udp->sock, IPPROTO_IP, IP_DROP_SOURCE_MEMBERSHIP, (char *)&mreqsrc, sizeof(mreqsrc)) != 0) { report("IP_DROP_SOURCE_MEMBERSHIP failed"); } } } /* close socket*/ #ifdef SYS_WINDOWS closesocket(udp->sock); WSACleanup(); #else close(udp->sock); #endif free(udp); return 0; }
/* if not return 0, it's up to the caller to free the uninserted node! */ int zlst_insert(void *PHEAD, void *ZNODE) { struct znode *head; struct znode *znode; if(!ZNODE) { RPT(RPT_ERR, "insert: bad node"); return -1; } znode = (struct znode *)ZNODE; if(!PHEAD) { RPT(RPT_ERR, "insert: NOT a list"); return -1; } head = *(struct znode **)PHEAD; if(!head) { RPT(RPT_INF, "insert: into empty list"); znode->tail = znode; znode->next = NULL; znode->prev = NULL; *(struct znode **)PHEAD = znode; return 0; } if(head->key > znode->key) { RPT(RPT_INF, "insert: %d as head before %d", znode->key, head->key); znode->tail = head->tail; znode->next = head; znode->prev = NULL; head->prev = znode; *(struct znode **)PHEAD = znode; return 0; } for(struct znode *x = head; x; x = x->next) { if(x->key == znode->key) { RPT(RPT_INF, "insert: %d in list already", znode->key); return -1; } if(x->key > znode->key) { RPT(RPT_INF, "insert: %d before %d", znode->key, x->key); znode->next = x; znode->prev = x->prev; x->prev->next = znode; x->prev = znode; return 0; } } RPT(RPT_INF, "insert: %d as tail after %d", znode->key, head->tail->key); znode->next = NULL; znode->prev = head->tail; head->tail->next = znode; head->tail = znode; return 0; }
inline std::pair< std::map< std::string, int >, RestartSchedule > RPTSCHED( const DeckKeyword& keyword ) { auto mnemonics = RPT( keyword, is_RPTSCHED_mnemonic, RPTSCHED_integer ); if( mnemonics.count( "NOTHING" ) ) return { std::move( mnemonics ), { 0 } }; if( mnemonics.count( "RESTART" ) ) return { std::move( mnemonics ), size_t( mnemonics.at( "RESTART" ) ) }; return { std::move( mnemonics ), {} }; }
static int report(const char *str) { int err; #ifdef SYS_WINDOWS err = WSAGetLastError(); #else err = errno; #endif RPT(RPT_ERR, "%s, errno: %d", str, err); return 0; }
inline std::pair< std::map< std::string, int >, RestartSchedule > RPTRST( const DeckKeyword& keyword, RestartSchedule prev, size_t step ) { auto mnemonics = RPT( keyword, is_RPTRST_mnemonic, RPTRST_integer ); const bool has_freq = mnemonics.find( "FREQ" ) != mnemonics.end(); const bool has_basic = mnemonics.find( "BASIC" ) != mnemonics.end(); expand_RPTRST_mnemonics( mnemonics ); if( !has_freq && !has_basic ) return { std::move( mnemonics ), {} }; const auto basic = has_basic ? mnemonics.at( "BASIC" ) : prev.basic; const auto freq = has_freq ? mnemonics.at( "FREQ" ) : prev.frequency; return { std::move( mnemonics ), { step, basic, freq } }; }
size_t udp_write(intptr_t id, const void *buf, int len) { struct udp *udp = (struct udp *)id; if(NULL == udp) { RPT(RPT_ERR, "bad id"); return -1; } size_t rslt = 0; rslt = sendto(udp->sock, buf, len, 0, (struct sockaddr *)&(udp->remote), udp->socklen); return rslt; }
/* it's up to the caller to free the node! */ void *zlst_delete(void *PHEAD, void *ZNODE) { struct znode *head; struct znode *znode; if(!ZNODE) { RPT(RPT_ERR, "delete: bad node"); return NULL; } znode = (struct znode *)ZNODE; if(!PHEAD) { RPT(RPT_ERR, "delete: NOT a list"); return NULL; } head = *(struct znode **)PHEAD; if(!head) { RPT(RPT_ERR, "delete: from an empty list"); return znode; /* free the node please */ } if(!(znode->prev) && !(znode->next)) { RPT(RPT_INF, "delete: from one node list"); *(struct znode **)PHEAD = NULL; return znode; } if(!(znode->prev) && (znode->next)) { RPT(RPT_INF, "delete: head node"); znode->next->tail = znode->tail; znode->next->prev = NULL; *(struct znode **)PHEAD = znode->next; return znode; } if((znode->prev) && !(znode->next)) { RPT(RPT_INF, "delete: tail node"); head->tail = znode->prev; znode->prev->next = NULL; return znode; } /* (znode->prev) && (znode->next) */ RPT(RPT_INF, "delete: ok"); znode->prev->next = znode->next; znode->next->prev = znode->prev; return znode; }
// The function programs the CSP of the radio module. // The CSP can run programs of up to 24 instructions. // Each instruction is written to register RFST in execution order void rf_prog() { // RFST = LABEL; //set next instruction as start of loop // RFST = SKIP(1,0,5); //skip 1 instructions if condition 5 (CSPY==0) applies (n=0) // RFST = STXON; //start TX // RFST = WAITX; //wait for MAC TIMER to overflow no. of times set in CSPX // RFST = SRFOFF; //stop TX/RX // RFST = WAIT(31); //wait for MAC TIMER to overflow 10 times // RFST = DECY; //CSPY-- // RFST = SKIP(1,1,5); //skip 1 instructions if condition 5 (CSPY==0) doesn't apply (n=1) // RFST = SKIP(0,0,5); //if(y==0) wait for value of CSPY to update // RFST = RPT(0,6); //goto start of loop if condition 6 (CSPZ==0) applies (n=0) RFST = LABEL; //set next instruction as start of loop RFST = SKIP(1,0,5); //skip 1 instructions if condition 5 (CSPY==0) applies (n=0) RFST = STXON; //start TX RFST = WEVENT1; //wait for TIMER2 event1 RFST = SRFOFF; //stop TX/RX RFST = WEVENT2; //wait for TIMER2 event2 RFST = DECY; //CSPY-- RFST = RPT(0,6); //goto start of loop if condition 6 (CSPZ==0) applies (n=0) }
intptr_t udp_open(char *src_addr, char *addr, unsigned short port, char *mode) { struct udp *udp; udp = (struct udp *)malloc(sizeof(struct udp)); if(NULL == udp) { RPT(RPT_ERR, "malloc failed"); return (intptr_t)NULL; } strcpy(udp->addr, addr); udp->src_addr[0] = '\0'; if(src_addr) { strcpy(udp->src_addr, src_addr); } #ifdef SYS_WINDOWS WSADATA wsaData; if(WSAStartup(MAKEWORD(1,1), &wsaData) == SOCKET_ERROR) { RPT(RPT_ERR, "WSAStartup error"); return (intptr_t)NULL; } #endif /* build socket */ if((udp->sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { report("socket failed"); return (intptr_t)NULL; } /* nonblock mode */ #ifdef SYS_WINDOWS { unsigned long opt = 1; ioctlsocket(udp->sock, FIONBIO, &opt); } #else { fcntl(udp->sock, F_SETFL, O_NONBLOCK); } #endif /* reuse address */ { int reuseaddr = 1; /* nonzero means enable */ setsockopt(udp->sock, SOL_SOCKET, SO_REUSEADDR, (char *)&reuseaddr, sizeof(int)); } /* name the socket */ { struct sockaddr_in local; memset(&local, 0, sizeof(local)); /* for some special compile environment */ local.sin_family = AF_INET; local.sin_addr.s_addr = htonl(INADDR_ANY); local.sin_port = ('r' == mode[0]) ? htons(port) : 0; if(bind(udp->sock, (struct sockaddr *)&local, sizeof(struct sockaddr)) < 0) { report("bind failed"); fprintf(stderr, "addr: %s, port: %d\n", inet_ntoa(local.sin_addr), ntohs(local.sin_port)); return (intptr_t)NULL; } } /* set the remote */ if('w' == mode[0]) { udp->remote.sin_family = AF_INET; udp->remote.sin_addr.s_addr = inet_addr(addr); udp->remote.sin_port = htons(port); } /* manage multicast */ if(IN_MULTICAST(ntohl(inet_addr(udp->addr)))) { if('\0' == udp->src_addr[0]) { struct ip_mreq imreq; RPT(RPT_INF, "IP_ADD_MEMBERSHIP: %s", udp->addr); imreq.imr_interface.s_addr = htonl(INADDR_ANY); imreq.imr_multiaddr.s_addr = inet_addr(udp->addr); if(setsockopt(udp->sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&imreq, sizeof(imreq)) != 0) { report("IP_ADD_MEMBERSHIP failed"); } } else { struct ip_mreq_source mreqsrc; RPT(RPT_INF, "IP_ADD_SOURCE_MEMBERSHIP: %s@%s", udp->src_addr, udp->addr); mreqsrc.imr_interface.s_addr = htonl(INADDR_ANY); mreqsrc.imr_multiaddr.s_addr = inet_addr(udp->addr); mreqsrc.imr_sourceaddr.s_addr = inet_addr(udp->src_addr); if(setsockopt(udp->sock, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP, (char *)&mreqsrc, sizeof(mreqsrc)) != 0) { report("IP_ADD_SOURCE_MEMBERSHIP failed"); } } } udp->socklen = sizeof(struct sockaddr_in); return (intptr_t)udp; }