layer_state_t contiki_io_layer_connect( layer_connectivity_t* context , const void* data , const layer_hint_t hint ) { XI_UNUSED( hint ); uip_ipaddr_t *ip; resolv_status_t dns_status; xi_connection_data_t* connection_data; layer_t* layer = ( layer_t* ) context->self; contiki_data_t* s = (contiki_data_t* ) layer->user_data; struct uip_conn *c; s->process = PROCESS_CURRENT(); if (s->state == CONNECTED) { xi_debug_logger( "Connecting to the endpoint [ok]" ); return LAYER_STATE_OK; } else if (s->state == CONNECTING) { return LAYER_STATE_WANT_WRITE; } else if (s->state == CLOSED) { connection_data = ( xi_connection_data_t* ) data; dns_status = resolv_lookup(connection_data->address, &ip); if (dns_status != RESOLV_STATUS_CACHED) { if (dns_status == RESOLV_STATUS_NOT_FOUND || dns_status == RESOLV_STATUS_ERROR) { xi_debug_logger( "Getting Host by name [failed]" ); xi_set_err( XI_SOCKET_GETHOSTBYNAME_ERROR ); return LAYER_STATE_ERROR; } if (dns_status != RESOLV_STATUS_RESOLVING) { resolv_query(connection_data->address); } return LAYER_STATE_WANT_WRITE; /* no IP, cannot go further */ } xi_debug_logger( "Getting Host by name [ok]" ); xi_debug_logger( "Connecting to the endpoint..." ); c = uip_connect(ip, uip_htons(connection_data->port)); if(c == NULL) { xi_debug_logger( "Connecting to the endpoint [failed]" ); xi_set_err( XI_SOCKET_CONNECTION_ERROR ); return LAYER_STATE_ERROR; } s->state = CONNECTING; c->appstate.p = &xively_process; c->appstate.state = s; tcpip_poll_tcp(c); return LAYER_STATE_WANT_WRITE; } return LAYER_STATE_ERROR; }
/*---------------------------------------------------------------------------*/ static void pollhandler(void) { if(ctk_mode_get() == CTK_MODE_EXTERNAL) { if(CIA1.prb == 0x7f) { ctkmode(); } else if(kbhit() && conn != NULL) { tcpip_poll_tcp(conn); } } }
// Disconnect from the server int mqtt_disconnect() { if(!process_is_running(&mqtt_process)) return -1; printf("mqtt: exiting...\n"); mqtt_flags &= ~MQTT_FLAG_READY; mqtt_flags |= MQTT_FLAG_EXIT; tcpip_poll_tcp(mqtt_state.tcp_connection); return 0; }
int mqtt_unsubscribe(const char* topic) { if(!mqtt_ready()) return -1; printf("sending unsubscribe\n"); mqtt_state.outbound_message = mqtt_msg_unsubscribe(&mqtt_state.mqtt_connection, topic, &mqtt_state.pending_msg_id); mqtt_flags &= ~MQTT_FLAG_READY; mqtt_state.pending_msg_type = MQTT_MSG_TYPE_UNSUBSCRIBE; tcpip_poll_tcp(mqtt_state.tcp_connection); return 0; }
struct uip_conn * tcp_connect(uip_ipaddr_t *ripaddr, uint16_t port, void *appstate) { struct uip_conn *c; c = uip_connect(ripaddr, port); if(c == NULL) { return NULL; } c->appstate.p = PROCESS_CURRENT(); c->appstate.state = appstate; tcpip_poll_tcp(c); return c; }
// Publish the specified message int mqtt_publish_with_length(const char* topic, const char* data, int data_length, int qos, int retain) { if(!mqtt_ready()) return -1; printf("mqtt: sending publish...\n"); mqtt_state.outbound_message = mqtt_msg_publish(&mqtt_state.mqtt_connection, topic, data, data_length, qos, retain, &mqtt_state.pending_msg_id); mqtt_flags &= ~MQTT_FLAG_READY; mqtt_state.pending_msg_type = MQTT_MSG_TYPE_PUBLISH; tcpip_poll_tcp(mqtt_state.tcp_connection); return 0; }
struct uip_conn * tcp_connect(uip_ipaddr_t *ripaddr, uint16_t port, void *appstate) { struct uip_conn *c; c = uip_connect(ripaddr, port); if(c == NULL) { return NULL; } c->appstate.state = appstate; c->appstate.conn_id = last_conn_id++; tcpip_poll_tcp(c); return c; }
/*-----------------------------------------------------------------------------------*/ uint16_t websocket_http_client_send(struct websocket_http_client_state *s, const uint8_t *data, uint16_t datalen) { int len; len = MIN(datalen, sizeof(s->outputbuf) - s->outputbufptr); if(len != datalen) { /* Could not send, return the length of the available data buffer instead. */ printf("websocket_http_client_send: could not send %d bytes, only %d available\n", datalen, len); return len; } /* printf("Putting %d bytes of data on outputbuf, ptr is %d\n", len, s->outputbufptr);*/ memcpy(&s->outputbuf[s->outputbufptr], data, len); s->outputbufptr += len; tcpip_poll_tcp(s->conn); return len; }