/*---------------------------------------------------------------------------*/ static void tcpip_handler(void) { char *str; if(uip_newdata()) { str = uip_appdata; str[uip_datalen()] = '\0'; printf("Response from the server: '%s'\n", str); } }
/*----------------------------------------------------------------------------*/ static void _demo_udp_callback(c_event_t c_event, p_data_t p_data) { char *pc_str; if(c_event == EVENT_TYPE_TCPIP) { if(uip_newdata()) { pc_str = uip_appdata; pc_str[uip_datalen()] = '\0'; LOG_INFO("Packet from a client: '%s'\n\r", pc_str); _demo_udp_sendMsg(_demo_extractSeq(pc_str) + 1); } } }
/*---------------------------------------------------------------------------*/ static void dtls_handle_read(dtls_context_t *ctx) { session_t session; if(uip_newdata()) { dtls_session_init(&session); uip_ipaddr_copy(&session.addr, &UIP_IP_BUF->srcipaddr); session.port = UIP_UDP_BUF->srcport; dtls_handle_message(ctx, &session, uip_appdata, uip_datalen()); } }
void handle_tcp_srv_app1(void) { static struct timer user_timer; //create a timer; static bool app_init = FALSE; if (uip_newdata()) { printf_high("Server RX [%d] bytes\n", uip_datalen()); iot_uart_output(uip_appdata, uip_datalen()); } if (uip_poll()) { /* below codes shows how to send data to client */ if ((app_init == FALSE) || timer_expired(&user_timer)) { printf_high("TCP SERVER APP1 uip_poll_timer_expired\n"); uip_send("hello,this is tcp srv...", 24); timer_set(&user_timer, 5*CLOCK_SECOND); app_init = TRUE; } } }
/*---------------------------------------------------------------------------*/ static void tcpip_handler(void) { char *str; if(uip_newdata()) { str = uip_appdata; str[uip_datalen()] = '\0'; printf("DATA recv '%s'\n", str); } }
/*---------------------------------------------------------------------------*/ static void man_udp_handler(void) { if(uip_newdata()) { /* ((char *)uip_appdata)[uip_datalen()] = 0; printf("New uIP data: '%s'\n", uip_appdata); PRINT6ADDR(&man_conf->mnaddr); printf("port = %u\n", man_conf->mnrport); */ char rst; rst = man_processing((uint8_t*)uip_appdata, uip_datalen(), &man_conf, client_conn); PRINTF("ManUDP handler return with %d\n", rst); } }
static u8_t parse_msg (void) { dhcpMsg_t *m = (dhcpMsg_t *) uip_appdata; if ((m->op == DHCP_REPLY) && !memcmp (m->xid, xid, sizeof (xid)) && !memcmp (m->chaddr, dhcpcState->mac_addr, dhcpcState->mac_len)) { memcpy (dhcpcState->ipaddr, m->yiaddr, 4); return parse_options (&m->options [4], uip_datalen ()); } return 0; }
/*---------------------------------------------------------------------------*/ static u8_t parse_msg(void) { struct dhcp_msg *m = (struct dhcp_msg *)uip_appdata; if(m->op == DHCP_REPLY && memcmp(m->xid, xid, sizeof(xid)) == 0 && memcmp(m->chaddr, s.mac_addr, s.mac_len) == 0) { memcpy(s.ipaddr, m->yiaddr, 4); return parse_options(&m->options[4], uip_datalen()); } return 0; }
static uint8_t parse_msg(void) { struct dhcp_msg *m = (struct dhcp_msg *)uip_appdata; if(m->op == DHCP_BOOTREPLY && memcmp(m->xid, &xid, sizeof(xid)) == 0 && memcmp(m->chaddr, dhcpc_state.mac_addr, dhcpc_state.mac_len) == 0) { memcpy(dhcpc_state.ipaddr.u16, m->yiaddr, 4); return parse_options(&m->options[4], uip_datalen()); } return 0; }
/*---------------------------------------------------------------------------*/ static void tcpip_handler(void) { char *appdata; if(uip_newdata()) { appdata = (char *)uip_appdata; appdata[uip_datalen()] = 0; PRINTF("DATA recv '%s' from ", appdata); PRINTF("%d", UDP_IP_BUF->srcipaddr.u8[sizeof(UDP_IP_BUF->srcipaddr.u8) - 1]); PRINTF("\n"); #if SERVER_REPLY PRINTF("DATA sending reply\n"); //uip_ipaddr_copy(&server_conn->ripaddr, &UDP_IP_BUF->srcipaddr); //server_conn->rport = UDP_IP_BUF->srcport; //uip_udp_packet_send(server_conn, appdata,uip_datalen() ); uip_udp_packet_sendto(server_conn, appdata,uip_datalen(),&UDP_IP_BUF->srcipaddr, UDP_IP_BUF->srcport); //uip_create_unspecified(&server_conn->ripaddr); #endif } }
/*---------------------------------------------------------------------------*/ static void tcpip_handler(void) { char *ptr; char *str; if(uip_newdata()) { leds_on(LEDS_BLUE); str = uip_appdata; str[uip_datalen()] = '\0'; printf("DATA recv %s\n", str); } }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(tcp_server, ev, data) { PROCESS_BEGIN(); tcp_listen(UIP_HTONS(8080)); while(1) { PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event); printf("TCP server: receive TCP event\n"); if(uip_closed() || uip_aborted() || uip_timedout()) { printf("TCP server: connection closed\n"); } else if(uip_connected()) { printf("TCP server: connected\n"); } if(uip_newdata()) { printf("TCP server: receive new data\n"); uint16_t len = uip_datalen(); uint8_t *ptr = uip_appdata; while(len--){ printf("%c", *ptr++); } printf("\n"); printf("TCP server: send echo message\n"); uip_send(uip_appdata, uip_datalen()); } if(uip_rexmit() || uip_newdata() || uip_acked() || uip_connected() || uip_poll()) { //senddata(); } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ void _udp_sock_callback(c_event_t c_event, p_data_t p_data) { struct udp_socket *c; if(c_event == EVENT_TYPE_TCPIP) { /* An appstate pointer is passed to use from the IP stack through the 'data' pointer. We registered this appstate when we did the udp_new() call in udp_socket_register() as the struct udp_socket pointer. So we extract this pointer and use it when calling the reception callback. */ c = (struct udp_socket *)p_data; /* Defensive coding: although the appstate *should* be non-null here, we make sure to avoid the program crashing on us. */ if(c != NULL) { LOG_INFO("socket ptr from callback = %p:%p", c, c->input_callback); /* If we were called because of incoming data, we should call the reception callback. */ if(uip_newdata()) { /* Copy the data from the uIP data buffer into our own buffer to avoid the uIP buffer being messed with by the callee. */ memcpy(buf, uip_appdata, uip_datalen()); /* Call the client process. We use the PROCESS_CONTEXT mechanism to temporarily switch process context to the client process. */ if(c->input_callback != NULL) { c->input_callback(c, c->ptr, &(UIP_IP_BUF->srcipaddr), UIP_HTONS(UIP_IP_BUF->srcport), &(UIP_IP_BUF->destipaddr), UIP_HTONS(UIP_IP_BUF->destport), buf, uip_datalen()); } } } } }
/*---------------------------------------------------------------------------*/ static uint8_t parse_msg(void) { struct dhcp_msg *m = (struct dhcp_msg *)uip_appdata; if(m->op == DHCP_REPLY && memcmp(m->xid, xid, sizeof(xid)) == 0 && memcmp(m->chaddr, uip_ethaddr.addr, sizeof(struct uip_eth_addr)) == 0) { memcpy(uip_udp_conn->appstate.dhcp.ipaddr, m->yiaddr, 4); return parse_options(&m->options[4], uip_datalen()); } return 0; }
/*----------------------------------------------------------------------------*/ static void _demo_udp_callback(c_event_t c_event, p_data_t p_data) { char *pc_str; if (etimer_expired(&st_et)) { _demo_udp_sendMsg(l_lastSeqId); etimer_restart(&st_et); } else if (c_event == EVENT_TYPE_TCPIP) { if (uip_newdata()) { pc_str = uip_appdata; pc_str[uip_datalen()] = '\0'; LOG_INFO("Packet from a server: '%s'\n\r", pc_str); l_lastSeqId = _demo_extractSeq(pc_str); } } }
/*---------------------------------------------------------------------------*/ static void net_input(void) { if(uip_newdata()) { memset(buffer, 0, MAX_MSG_SIZE); msg_len = MIN(uip_datalen(), MAX_MSG_SIZE - 1); /* Copy data */ memcpy(buffer, uip_appdata, msg_len); printf("%s", (char *)buffer); } return; }
bool readUDP(NetworkSocket * networkSocket, uint8_t * buffer, int bufferLength, NetworkAddress ** sourceAddress, int *readLength) { bool result = true; *readLength = 0; //if (uip_newdata() && (UIP_IP_BUF->destport == networkSocket->Port) ) if (uip_newdata()) { Lwm2m_Debug("Packet from: %d %d\n", uip_htons(UIP_IP_BUF->destport), networkSocket->Port); if (uip_datalen() > bufferLength) *readLength = bufferLength; else *readLength = uip_datalen(); } if (*readLength > 0) { uip_ipaddr_t * address = &UIP_IP_BUF->srcipaddr; uint16_t port = uip_htons(UIP_IP_BUF->srcport); bool secure = (networkSocket->SocketType & NetworkSocketType_Secure) == NetworkSocketType_Secure; memcpy(buffer, uip_appdata, *readLength); NetworkAddress * networkAddress = getCachedAddress(address, port); if (networkAddress == NULL) { networkAddress = addCachedAddress(address, port, secure); if (networkAddress) { networkAddress->useCount++; // TODO - ensure addresses are freed? (after t/o or transaction or DTLS session closed) } } if (networkAddress) { *sourceAddress = networkAddress; } } return result; }
/*---------------------------------------------------------------------------*/ static void tcpip_handler(void) { char *appdata; unsigned char i; blink_event = process_alloc_event(); if(uip_newdata()) { appdata = (char *)uip_appdata; appdata[uip_datalen()] = 0; for(i = 0; i < 4; i++){ dht11data[i] = *(appdata + i); } PRINTF("DATA recv '%s' from ", appdata); PRINTF("%d", UIP_IP_BUF->srcipaddr.u8[sizeof(UIP_IP_BUF->srcipaddr.u8) - 1]); PRINTF("\n"); PRINTF("wendu is %c%c, shidu is %c%c \n", *appdata,*(appdata+1),*(appdata+2),*(appdata+3)); // PRINTF("DATA sending reply\n"); // uip_ipaddr_copy(&server_conn->ripaddr, &UIP_IP_BUF->srcipaddr); /* if(strncmp(appdata, "RED", 3) == 0) { leds_on(LEDS_RED); leds_off(LEDS_GREEN); sta = 0; } if(strncmp(appdata, "GREEN", 5) == 0) { leds_on(LEDS_GREEN); leds_off(LEDS_RED); sta = 0; } if(strncmp(appdata,"BLINK",5) == 0) { process_post(&udp_server_process, blink_event, NULL); sta = 1; } if(strncmp(appdata,"OFF",3) == 0) { leds_off(LEDS_RED | LEDS_GREEN); sta = 0; } #if SERVER_REPLY PRINTF("DATA sending reply\n"); uip_ipaddr_copy(&server_conn->ripaddr, &UIP_IP_BUF->srcipaddr); uip_udp_packet_send(server_conn, "Reply", sizeof("Reply")); uip_create_unspecified(&server_conn->ripaddr); #endif */ } }
/*---------------------------------------------------------------------------*/ static uint8_t parse_msg(struct ntp_time *time) { struct ntp_msg *m = (struct ntp_msg *)uip_appdata; // check correct time len if(uip_datalen() != sizeof(struct ntp_msg)) return 0; // adjust endianess time->seconds = ntp_read_u32(m->transmit_timestamp[0]); time->fraction = ntp_read_u32(m->transmit_timestamp[1]) >> 16; return 1; }
/*---------------------------------------------------------------------------*/ static void tcpip_handler(void) { leds_on(LEDS_GREEN); if(uip_newdata()) { putstring("0x"); puthex(uip_datalen()); putstring(" bytes response=0x"); puthex((*(uint16_t *) uip_appdata) >> 8); puthex((*(uint16_t *) uip_appdata) & 0xFF); putchar('\n'); } leds_off(LEDS_GREEN); return; }
/*--------------------------------------------------------------------------------------------*/ void tcpip_handler(struct udp_tx *udp_tx_info){ if(uip_newdata()){ ((char *)uip_appdata)[uip_datalen()] = 0; PRINTF("Server received: '%s' from [", (char *)uip_appdata); PRINT6ADDR(&UIP_IP_BUF->srcipaddr); PRINTF("]:%u",UIP_HTONS(UIP_UDP_BUF->srcport)); PRINTF("\n"); uip_ipaddr_copy(&server_conn->ripaddr, &UIP_IP_BUF->srcipaddr); server_conn->rport=uip_htons(3007); PRINTF("Responding with message: "); udp_data_handler(uip_appdata,udp_tx_info); } }
uint64_t _demo_extractSeq(char * str) { char temp[20]; uint8_t i = 0; //uint8_t j = 0; for (i=uip_datalen();i>0;i--) { if (str[i] == '|' ) { i--; memcpy(temp,str,i); temp[i] = '\0'; break; } } return atol(temp); }
void uecmd_net_main() { if (!uip_newdata ()) return; char *p = (char *)uip_appdata; /* Add \0 to the data and remove \n from the data */ do { if (*p == '\r' || *p == '\n') { break; } } while ( ++p <= ((char *)uip_appdata + uip_datalen())); /* Parse the Data */ *p = 0; char cmd[p - (char *)uip_appdata]; strncpy(cmd, uip_appdata, p - (char *)uip_appdata + 1); uip_slen = 0; while (uip_slen < UIP_BUFSIZE - UIP_IPUDPH_LEN) { int16_t len = ecmd_parse_command(cmd, ((char *)uip_appdata) + uip_slen, (UIP_BUFSIZE - UIP_IPUDPH_LEN) - uip_slen); uint8_t real_len = len; if (!is_ECMD_FINAL(len)) { /* what about the errors ? */ /* convert ECMD_AGAIN back to ECMD_FINAL */ real_len = (uint8_t) ECMD_AGAIN(len); } uip_slen += real_len + 1; ((char *)uip_appdata)[uip_slen - 1] = '\n'; if (real_len == len || len == 0) break; } /* Sent data out */ uip_udp_conn_t echo_conn; uip_ipaddr_copy(echo_conn.ripaddr, BUF->srcipaddr); echo_conn.rport = BUF->srcport; echo_conn.lport = HTONS(ECMD_UDP_PORT); uip_udp_conn = &echo_conn; uip_process(UIP_UDP_SEND_CONN); router_output(); uip_slen = 0; }
static unsigned char parse_msg(void) { int result = 0; int bytes_available = uip_datalen(); unsigned char* pData = uip_appdata; result = 1; lo_message message = lo_message_deserialise(pData, bytes_available, &result); if (result == 0) { //char* path = lo_url_get_path(lo_address_get_url(lo_message_get_source(message))); lo_arg** argv = lo_message_get_argv(message); //if (!strcmp(path,'/set/rgb')) { lo_arg* red = argv[0]; lo_arg* green = argv[1]; lo_arg* blue = argv[2]; float fRed = red->f; float fGreen = green->f; float fBlue = blue->f; analogWrite(redPin, (unsigned char)(fRed * 0xFF)); analogWrite(greenPin, (unsigned char)(fGreen * 0xFF)); analogWrite(bluePin, (unsigned char)(fBlue * 0xFF)); /*} else if (!strcmp(path, '/set/hsi')) { lo_arg* H = argv[0]; lo_arg* S = argv[1]; lo_arg* I = argv[2]; unsigned char rgb[3]; hsi2rgb(H, S, I, rgb); analogWrite(redPin, rgb[0]); analogWrite(greenPin, rgb[1]); analogWrite(bluePin, rgb[2]); }*/ } lo_message_free(message); s.state = STATE_QUIT; return 1; }
/*---------------------------------------------------------------------*/ PROCESS_THREAD(test_uaodv_process, ev, data) { static uip_ipaddr_t addr; PROCESS_BEGIN(); printf("uIP uAODV test process started\n"); uip_ipaddr(&addr, 0,0,0,0); in_conn = udp_new(&addr, UIP_HTONS(0), NULL); uip_udp_bind(in_conn, UIP_HTONS(COOJA_PORT)); uip_ipaddr(&addr, 10,10,10,4); out_conn = udp_new(&addr, UIP_HTONS(COOJA_PORT), NULL); button_sensor.configure(SENSORS_ACTIVE, 1); while(1) { PROCESS_WAIT_EVENT(); if(ev == sensors_event && data == &button_sensor) { struct uaodv_rt_entry *route; uip_ipaddr(&addr, 10,10,10,4); route = uaodv_rt_lookup_any(&addr); if (route == NULL || route->is_bad) { printf("%d.%d.%d.%d: lookup %d.%d.%d.%d\n", uip_ipaddr_to_quad(&uip_hostaddr), uip_ipaddr_to_quad(&addr)); uaodv_request_route_to(&addr); } else { printf("%d.%d.%d.%d: send to %d.%d.%d.%d\n", uip_ipaddr_to_quad(&uip_hostaddr), uip_ipaddr_to_quad(&addr)); tcpip_poll_udp(out_conn); PROCESS_WAIT_UNTIL(ev == tcpip_event && uip_poll()); uip_send("cooyah COOJA", 12); } } if(ev == tcpip_event && uip_newdata()) { ((char*) uip_appdata)[uip_datalen()] = 0; printf("data received from %d.%d.%d.%d: %s\n", uip_ipaddr_to_quad(&((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])->srcipaddr), (char *)uip_appdata); leds_toggle(LEDS_ALL); } } PROCESS_END(); }
static void newdata(void) { u16_t len; len = uip_datalen(); if (s.state == WEBCLIENT_STATE_STATUSLINE) len = parse_statusline(len); if (s.state == WEBCLIENT_STATE_HEADERS && len > 0) len = parse_headers(len); if (len > 0 && s.state == WEBCLIENT_STATE_DATA && s.httpflag != HTTPFLAG_MOVED) webclient_datahandler((char *)uip_appdata, len); }
void process1_app(void){ u16_t len; /*if(uip_connected() && uip_conn->lport == HTONS(55555)) { ser_print("1. Accepted connection on port 55555\n"); }*/ if(uip_newdata()) { len = uip_datalen(); memcpy(incoming, uip_appdata, len); incoming[len] = '\0'; } if(sendData != 0 && uip_conn->lport == HTONS(55555)){ //ser_print("Sending status...\n"); sprintf(temp, "%d", doorStatus); uip_send(temp, 1); sendData = 0; } }
/* * In socketapp.h we have defined the UIP_APPCALL macro to * socket_app_appcall so that this function is uIP's application * function. This function is called whenever an uIP event occurs * (e.g. when a new connection is established, new data arrives, sent * data is acknowledged, data needs to be retransmitted, etc.). */ void socket_app_appcall( void ) { /* * The uip_conn structure has a field called "appstate" that holds * the application state of the connection. We make a pointer to * this to access it easier. */ struct socket_app_state *s = &(uip_conn->appstate); /* * If a new connection was just established, we should initialize * the protosocket in our applications' state structure. */ if ( uip_connected() ) { s->no = counter++; memset( buffer, 0, sizeof(buffer)); WiFi_connected( s->no ); } /** * Neue Daten anstehend */ else if ( uip_newdata() ) { memset( buffer, 0, sizeof(buffer) ); int length = uip_datalen(); int i = 0; while ( i < length && i < sizeof(buffer) ) { if ( ((char *) uip_appdata)[i] == '\r' ) continue; if ( ((char *) uip_appdata)[i] == '\n' ) break; buffer[i] = ((char *) uip_appdata)[i]; i++; } WiFi_newData( s->no, buffer, length ); } /** * Verbindung wird geschlossen */ else if ( uip_aborted() || uip_closed() ) { WiFi_closed( s->no ); } }
/*---------------------------------------------------------------------------*/ static void tcpip_handler(void) { uint8_t *appdata; linkaddr_t sender; uint8_t seqno; uint8_t hops; if(uip_newdata()) { appdata = (uint8_t *)uip_appdata; sender.u8[0] = UIP_IP_BUF->srcipaddr.u8[15]; sender.u8[1] = UIP_IP_BUF->srcipaddr.u8[14]; seqno = *appdata; hops = uip_ds6_if.cur_hop_limit - UIP_IP_BUF->ttl + 1; collect_common_recv(&sender, seqno, hops, appdata + 2, uip_datalen() - 2); } }
/*-----------------------------------------------------------------------------------*/ void telnet_app(void *ts) { struct telnet_state *s = (struct telnet_state *)ts; if(uip_connected()) { s->flags = 0; telnet_connected(s); senddata(s); return; } if(uip_closed()) { telnet_closed(s); } if(uip_aborted()) { telnet_aborted(s); } if(uip_timedout()) { telnet_timedout(s); } if(s->flags & FLAG_CLOSE) { uip_close(); return; } if(s->flags & FLAG_ABORT) { uip_abort(); return; } if(uip_acked()) { acked(s); } if(uip_newdata()) { telnet_newdata(s, (char *)uip_appdata, uip_datalen()); } if(uip_rexmit() || uip_newdata() || uip_acked()) { senddata(s); } else if(uip_poll()) { senddata(s); } }