void in_out_packet() { bool incoming = false; if (uip_len > 0) { NABTO_LOG_INFO(("incoming nabto traffic")); ipacket.socket = uip_udp_conn; ipacket.addr = ((uint32_t)uip_ipaddr1(BUF->srcipaddr) << 24) + ((uint32_t)uip_ipaddr2(BUF->srcipaddr) << 16) + ((uint32_t)uip_ipaddr3(BUF->srcipaddr) << 8) + (uint32_t)uip_ipaddr4(BUF->srcipaddr); ipacket.port = htons(BUF->srcport); ipacket.len = uip_len; memcpy(ipacket.buffer, uip_appdata, uip_len); incoming = true; } if (opacket.socket == uip_udp_conn) { // we send on the current connection NABTO_LOG_INFO(("outgoing nabto traffic")); opacket.socket=NULL; uip_ipaddr(&uip_udp_conn->ripaddr, opacket.addr>>24 & 255, opacket.addr>>16 & 255, opacket.addr>>8 & 255, opacket.addr & 255); uip_udp_conn->rport = htons(opacket.port); memcpy(uip_appdata, opacket.buffer, opacket.len); uip_udp_send(opacket.len); }
/** * This code makes a connection unique, we have tried to make * connections through several channels. But when we receives the * first real data message on one channel we know that the client * ended up using this specific channel. */ void nabto_connection_end_connecting(nabto_connect* con, message_event* event) { if (event->type == MT_UDP) { #if NABTO_ENABLE_TCP_FALLBACK unabto_tcp_fallback_close(con); #endif con->state = CS_CONNECTED; con->type = NCT_REMOTE_P2P; con->conAttr = CON_ATTR_DEFAULT; con->socket = event->udpMessage.socket; NABTO_LOG_TRACE(("Received data on socket %i", con->socket)); con->peer = event->udpMessage.peer; if (EP_EQUAL(event->udpMessage.peer, nmc.context.gsp)) { NABTO_LOG_INFO((PRInsi " UDP Fallback through the GSP ", MAKE_NSI_PRINTABLE(0, con->spnsi, 0))); } else { NABTO_LOG_INFO((PRInsi " Direct UDP connection", MAKE_NSI_PRINTABLE(0, con->spnsi, 0))); } } #if NABTO_ENABLE_TCP_FALLBACK if (event->type == MT_TCP_FALLBACK) { con->state = CS_CONNECTED; con->type = NCT_REMOTE_RELAY_MICRO; con->conAttr |= con->fbConAttr; NABTO_LOG_INFO((PRInsi " TCP FAllback connection ", MAKE_NSI_PRINTABLE(0, con->spnsi, 0))); } #endif nabto_rendezvous_end(con); con->sendConnectStatistics = true; }
// Initialise serial cable and run one time setup bool application_init(void) { char *portname = "/dev/ttyUSB0"; fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC); if (fd < 0) { NABTO_LOG_INFO(("error %d opening %s: %s\n", errno, portname, strerror (errno))); return 0; } else{ NABTO_LOG_INFO(("Opening %s: %s\n", portname, strerror (errno))); } set_interface_attribs (fd, B115200, 0); // set speed to 115,200 bps, 8n1 (no parity) set_blocking (fd, 0); // set no blocking // Wake Roomba setRTS(fd, 0); usleep(100000); //0.1 sec setRTS(fd, 1); sleep(2); // Songs can be defined while in passive mode (which we are in right now) char DSB[] = {140, 0, 3, 74, 40, 75, 20, 70, 32}; write(fd, &DSB, sizeof(DSB)); usleep ((sizeof(DSB)+25) * 100); char reverse[] = {140, 1, 6, 84, 32, 30, 32, 84, 32, 30, 32, 84, 32, 30, 32}; write(fd, &reverse, sizeof(reverse)); usleep ((sizeof(reverse)+25) * 100); char car_low[] = {140, 2, 12, 74,10, 71,10, 67,10, 67,10, 67,10, 69,10, 71,10, 72,10, 74,10, 74,10, 74,10, 71,10}; write(fd, &car_low, sizeof(car_low)); usleep ((sizeof(car_low)+25) * 100); char car_high[] = {140, 3, 12, 86,10, 83,10, 79,10, 79,10, 79,10, 81,10, 83,10, 84,10, 86,10, 86,10, 86,10, 83,10}; write(fd, &car_high, sizeof(car_high)); usleep ((sizeof(car_high)+25) * 100); // Put into safe mode char safe[] = {128, 131}; write(fd, &safe, sizeof(safe)); usleep(20*1000); // When changing mode, allow 20 milliseconds // Set display to NAb T/O char display[] = {163, 55, 119, 124, 93}; write(fd, &display, sizeof(display)); usleep ((sizeof(display)+25) * 100); return 1; }
/** * Entry point. * @param argc number of parameters * @param argv the parameters * - 1 ip-address * - 2 serverid | - * - 3 log * @return the result */ int main(int argc, char* argv[]) { nabto_main_setup* nms; nabto_endpoint localEp; // flush stdout setvbuf(stdout, NULL, _IONBF, 0); nms = unabto_init_context(); /** * Overwrite default values with command line args */ if (!check_args(argc, argv, nms)) { return 1; } if (!unabto_init()) { NABTO_LOG_FATAL(("Failed at nabto_main_init")); } localEp.addr=nms->ipAddress; localEp.port=nms->localPort; if (nms->ipAddress != INADDR_ANY) NABTO_LOG_INFO(("Own IP address: " PRIep, MAKE_EP_PRINTABLE(localEp))); else NABTO_LOG_INFO(("Own IP address, local IP is set to INADDR_ANY: " PRIep, MAKE_EP_PRINTABLE(localEp))); while (true) { /* * Here the main application should do it's work * and then whenever time is left for Nabto (but at least regularly) * the nabto_main_tick() should be called. * * nabto_main_tick() * - polls the socket for incoming messages * - administers timers to do its own timer based work */ unabto_tick(); #if NABTO_ENABLE_STREAM nabto_stream_test_tick(); #endif #if (_POSIX_C_SOURCE >= 199309L) struct timespec sleepTime; sleepTime.tv_sec = 0; sleepTime.tv_nsec = 10*1000000; nanosleep(&sleepTime, NULL); #endif } unabto_close(); return 0; }
application_event_result application_event(application_request* request, buffer_read_t* r_b, buffer_write_t* w_b) { if (request->queryId == 1 || request->queryId == 2 || request->queryId == 3) { NABTO_LOG_INFO(("Application event: Respond immediately")); return demo_application(request, r_b, w_b); } if (saved_app_req) { NABTO_LOG_INFO(("Application event: No resources")); return AER_REQ_OUT_OF_RESOURCES; } else { NABTO_LOG_INFO(("Application event: Accept request")); saved_app_req = request; return AER_REQ_ACCEPTED; } }
bool tunnel_allow_connection(const char* host, int port) { size_t i; bool allow; bool portFound = false; bool hostFound = false; if (!check_acl) { return true; } if (allow_all_ports) { portFound = true; } else { for (i = 0; i < ports_length; i++) { if (ports[i] == port) { portFound = true; } } } for(i = 0; i < hosts_length; i++) { if (strcmp(host, hosts[i]) == 0) { hostFound = true; } } allow = hostFound && portFound; if (!allow) { NABTO_LOG_INFO(("Current acl has disallowed access to %s:%i", host, port)); } return allow; }
bool unabto_dns_fallback_data_send_packet_start_request(unabto_dns_fallback_session* session, uint16_t chunks, nabto_endpoint* ep, uint16_t totalLength) { uint8_t requestBuffer[19]; uint8_t* ptr = requestBuffer; uint8_t* buffer; uint8_t* end; uint16_t id; size_t length; WRITE_FORWARD_U8(ptr, UDF_TYPE_SEND_START); WRITE_FORWARD_U16(ptr, 19); WRITE_FORWARD_U16(ptr, session->sessionId); WRITE_FORWARD_U16(ptr, session->random++); WRITE_FORWARD_U16(ptr, session->packetId); WRITE_FORWARD_U16(ptr, chunks); WRITE_FORWARD_U32(ptr, ep->addr); WRITE_FORWARD_U16(ptr, ep->port); WRITE_FORWARD_U16(ptr, totalLength); buffer = requestEncoded; end = buffer + ENCODED_BUFFER_SIZE; buffer = unabto_dns_fallback_encode_request_as_question(buffer, end, requestBuffer, 19, session->domain, &id); if (buffer == NULL) { return false; } length = buffer - requestEncoded; NABTO_LOG_INFO(("Sending DNS start request id: %" PRIu16, id)); nabto_write(session->dnsClient.sockets[(session->dnsClient.lastUsedSocket++) % DNS_CLIENT_SOCKETS], requestEncoded, length, session->dnsClient.dnsServer.addr, session->dnsClient.dnsServer.port); return true; }
bool unabto_dns_fallback_data_send_packet_chunk_request(unabto_dns_fallback_session* session, uint8_t* start, uint16_t dataStart, uint16_t length, uint16_t chunkId) { uint8_t* ptr = requestBuffer; size_t requestLength; uint8_t* buffer; uint8_t* end; uint16_t id; size_t packetLength; WRITE_FORWARD_U8(ptr, UDF_TYPE_SEND_CHUNK); WRITE_FORWARD_U16(ptr, length+13); WRITE_FORWARD_U16(ptr, session->sessionId); WRITE_FORWARD_U16(ptr, session->random++); WRITE_FORWARD_U16(ptr, session->packetId); WRITE_FORWARD_U16(ptr, chunkId); WRITE_FORWARD_U16(ptr, dataStart); memcpy(ptr, start, length); ptr += length; requestLength = ptr - requestBuffer; buffer = requestEncoded; end = buffer + ENCODED_BUFFER_SIZE; buffer = unabto_dns_fallback_encode_request_as_question(buffer, end, requestBuffer, requestLength, session->domain, &id); if (buffer == NULL) { return false; } packetLength = buffer - requestEncoded; NABTO_LOG_INFO(("Sending DNS chunk request id: %" PRIu16, id)); nabto_write(session->dnsClient.sockets[(session->dnsClient.lastUsedSocket++) % DNS_CLIENT_SOCKETS], requestEncoded, packetLength, session->dnsClient.dnsServer.addr, session->dnsClient.dnsServer.port); return true; }
void unabto_tunnel_failed_command(tunnel* tunnel, tunnel_event_source tunnel_event) { if (tunnel->state == TS_FAILED_COMMAND) { const uint8_t* buf; unabto_stream_hint hint; size_t readen; do { readen = unabto_stream_read(tunnel->stream, &buf, &hint); if (readen > 0) { unabto_stream_ack(tunnel->stream, buf, readen, &hint); } } while (readen > 0); if (unabto_stream_close(tunnel->stream)) { unabto_stream_stats info; unabto_stream_get_stats(tunnel->stream, &info); NABTO_LOG_TRACE(("Closed tunnel successfully")); NABTO_LOG_INFO(("Tunnel(%i) closed, sentPackets: %u, sentBytes %u, sentResentPackets %u, receivedPackets %u, receivedBytes %u, receivedResentPackets %u, reorderedOrLostPackets %u", tunnel->tunnelId, info.sentPackets, info.sentBytes, info.sentResentPackets, info.receivedPackets, info.receivedBytes, info.receivedResentPackets, info.reorderedOrLostPackets)); unabto_stream_release(tunnel->stream); unabto_tunnel_reset_tunnel_struct(tunnel); } } }
bool microchip_udp_open(uint32_t localAddr, uint16_t* localPort, UDP_SOCKET* socketDescriptor) { UDP_SOCKET sd; sd = UDPOpenEx(0, UDP_OPEN_SERVER, *localPort, 0); NABTO_LOG_INFO(("create socket...")); if (sd == INVALID_UDP_SOCKET) { NABTO_LOG_INFO(("failed to create socket")); return false; } *localPort = UDPSocketInfo[sd].localPort; *socketDescriptor = sd; // NABTO_LOG_INFO(("init socket %i port %i", *socketDescriptor, *localPort)); return true; }
void nabto_release_connection(nabto_connect* con) { if (con->state != CS_IDLE) { NABTO_LOG_INFO((PRInsi " Release connection (record %i)", MAKE_NSI_PRINTABLE(0, con->spnsi, 0), nabto_connection_index(con))); if (con->state != CS_CLOSE_REQUESTED) { con->state = CS_CLOSE_REQUESTED; #if NABTO_ENABLE_STREAM nabto_stream_connection_released(con); #endif } if (con->socket != NABTO_INVALID_SOCKET) { if (con->socket != nmc.socketGSP && con->socket != nmc.socketLocal) { nabto_close_socket(&con->socket); } } // in case the rendezvous state is still active. nabto_rendezvous_end(con); #if NABTO_ENABLE_TCP_FALLBACK unabto_tcp_fallback_close(con); #endif con->state = CS_IDLE; nabto_crypto_release(&con->cryptoctx); } else { NABTO_LOG_TRACE(("nabto_release_connection called on non used connection")); } }
void nabto_rendezvous_stop(nabto_connect* con) { nabto_rendezvous_connect_state* rcs = &con->rendezvousConnectState; if (rcs->state == RS_CONNECTING) { NABTO_LOG_INFO(("Ports opened: %i", rcs->portsOpened)); rcs->state = RS_DONE; } }
uint8_t setLight(uint8_t id, uint8_t onOff) { PD10 = !onOff; NABTO_LOG_INFO((onOff?("Nabto: Light turned ON!"):("Nabto: Light turned OFF!"))); #if NABTO_ENABLE_LOGGING == 0 print_Line(3, onOff?("Light ON "):("Light OFF")); #endif return onOff; }
/** * Echo using _all stream functionality. */ void *echo(void* params) { unabto_api_stream_t stream; memcpy(&stream, params, sizeof(unabto_api_stream_t)); uint8_t buf[BUFSIZE]; size_t bytes_read; for(;;) { if (unabto_api_stream_read(stream, buf, BUFSIZE, &bytes_read) != UNABTO_API_OK) { NABTO_LOG_INFO(("stream read failed")); break; } if (unabto_api_stream_write_all(stream, buf, bytes_read) != UNABTO_API_OK) { NABTO_LOG_INFO(("stream write failed")); break; } } return NULL; }
void unabto_stream_init(void) { memset(stream__, 0, sizeof(struct nabto_stream_s) * NABTO_MEMORY_STREAM_MAX_STREAMS); NABTO_LOG_INFO(("sizeof(stream__)=%" PRIsize, sizeof(struct nabto_stream_s) * NABTO_MEMORY_STREAM_MAX_STREAMS)); unabto_packet_set_handler(NP_PACKET_HDR_TAG_STREAM_MIN, NP_PACKET_HDR_TAG_STREAM_MAX, handle_stream_packet, 0); }
/** * main using gopt to check command line arguments * -h for help */ int main(int argc, char* argv[]) { // Set nabto to default values nabto_main_setup* nms = unabto_init_context(); // Overwrite default values with command line args if (!check_args(argc, argv, nms)) { return 1; } NABTO_LOG_INFO(("Identity: '%s'", nms->id)); NABTO_LOG_INFO(("Program Release %i.%i", RELEASE_MAJOR, RELEASE_MINOR)); NABTO_LOG_INFO(("Buffer size: %i", nms->bufsize)); // Initialize nabto if (!unabto_init()) { NABTO_LOG_FATAL(("Failed at nabto_main_init")); } nabto_stamp_t attachExpireStamp; // 20 seconds nabtoSetFutureStamp(&attachExpireStamp, 1000*20); // The main loop gives nabto a tick from time to time. // Everything else is taken care of behind the scenes. while (true) { unabto_tick(); nabto_yield(10); if (nabtoIsStampPassed(&attachExpireStamp)) { NABTO_LOG_ERROR(("Attach took too long.")); exit(4); } if (unabto_is_connected_to_gsp()) { NABTO_LOG_INFO(("Successfully attached to the gsp, now exiting.")); exit(0); } } NABTO_LOG_ERROR(("we should not end here")); exit(5); unabto_close(); return 0; }
void rendezvous_time_event(nabto_connect* con) { nabto_rendezvous_connect_state* rcs = &con->rendezvousConnectState; if (rcs->state == RS_CONNECTING) { if (nabtoIsStampPassed(&rcs->timestamp)) { send_rendezvous_to_all(con); } #if NABTO_ENABLE_EXTENDED_RENDEZVOUS_MULTIPLE_SOCKETS if (rcs->openManySockets && nabtoIsStampPassed(&rcs->openManySocketsStamp)) { if (rcs->socketsOpened < NABTO_EXTENDED_RENDEZVOUS_MAX_SOCKETS) { nabto_socket_t* candidate = &rcs->sockets[rcs->socketsOpened]; uint16_t localport = 0; if(nabto_init_socket(0,&localport, candidate)) { rcs->socketsOpened++; send_rendezvous_socket(*candidate, con, 0, &con->cp.globalEndpoint, 0); } else { NABTO_LOG_ERROR(("Could not open socket.")); } nabtoSetFutureStamp(&con->rendezvousConnectState.openManySocketsStamp, 20); } else { rcs->openManySockets = false; } } #endif if (rcs->openManyPorts && nabtoIsStampPassed(&rcs->openManyPortsStamp)) { int i; for (i = 0; i < 10; i++) { nabto_endpoint newEp; uint16_t newPort; nabto_random((uint8_t*)&newPort, sizeof(uint16_t)); newEp.addr = con->cp.globalEndpoint.addr; newEp.port = 1024+(newPort%64500); send_rendezvous(con, 0, &newEp, 0); rcs->portsOpened++; } nabtoSetFutureStamp(&rcs->openManyPortsStamp, 50); } if(nabtoIsStampPassed(&rcs->timeout)) { #if NABTO_ENABLE_EXTENDED_RENDEZVOUS_MULTIPLE_SOCKETS NABTO_LOG_INFO(("Rendezvous timeout. Sockets opened %i", rcs->socketsOpened)); #endif nabto_rendezvous_stop(con); } } }
bool check_syslog_state() { if (!syslog_enabled) { return false; } if (syslog_expire && nabtoIsStampPassed(&syslog_expire_stamp)) { unabto_log_system_disable_syslog(); NABTO_LOG_INFO(("Disabling syslog since it has expired")); } return syslog_enabled; }
void handle_signal(int signum) { if (signum == SIGTERM) { // Terminate application with exit, // This makes libprofiler extra happy NABTO_LOG_INFO(("Exit in signal handler: SIGTERM")); exit(1); } if (signum == SIGINT) { // Terminate application with exit, // This makes libprofiler extra happy if (nice_exit) { NABTO_LOG_INFO(("Exit in signal handler: SIGINT.")); NABTO_LOG_INFO(("Cleanup nicely (since --nice_exit was specified)")); unabto_close(); NABTO_LOG_INFO(("Cleanup done. Exiting")); exit(1); } NABTO_LOG_INFO(("Exit in signal handler: SIGINT. No --nice_exit argument -> no cleanup")); exit(1); } }
void rendezvous_time_event(nabto_connect* con) { nabto_rendezvous_connect_state* rcs = &con->rendezvousConnectState; if (rcs->state == RS_CONNECTING) { if (nabtoIsStampPassed(&rcs->timestamp)) { send_rendezvous_to_all(con); } #if NABTO_ENABLE_EXTENDED_RENDEZVOUS_MULTIPLE_SOCKETS if (rcs->openManySockets && nabtoIsStampPassed(&rcs->openManySocketsStamp)) { if (rcs->socketsOpened < NABTO_EXTENDED_RENDEZVOUS_MAX_SOCKETS) { nabto_socket_t* candidate = &extended_rendezvous_sockets[rcs->socketsOpened]; send_rendezvous_socket(*candidate, con, 0, &con->cp.globalEndpoint, 0); rcs->socketsOpened++; unabto_connection_set_future_stamp(&con->rendezvousConnectState.openManySocketsStamp, 20); } else { rcs->openManySockets = false; } } #endif if (rcs->openManyPorts && nabtoIsStampPassed(&rcs->openManyPortsStamp)) { int i; for (i = 0; i < 10; i++) { nabto_endpoint newEp; uint16_t newPort = unabto_extended_rendezvous_next_port(&rcs->portSequence, rcs->portsOpened); newEp.addr = con->cp.globalEndpoint.addr; newEp.port = newPort; send_rendezvous(con, 0, &newEp, 0); rcs->portsOpened++; } unabto_connection_set_future_stamp(&rcs->openManyPortsStamp, 50); } if(nabtoIsStampPassed(&rcs->timeout)) { #if NABTO_ENABLE_EXTENDED_RENDEZVOUS_MULTIPLE_SOCKETS NABTO_LOG_INFO(("Rendezvous timeout. Sockets opened %i", rcs->socketsOpened)); #endif nabto_rendezvous_stop(con); } } }
bool read_hex_test(void) { uint8_t expected1[8] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }; uint8_t expected2[0] = {}; NABTO_LOG_INFO(("read hex test")); return hex_test("", expected2, 0) && hex_test("0123456789abcdef", expected1, 8) && hex_test("0123456789abcdef0", expected1, 8) && negative_hex_test("fgh3456789abcdef"); }
/** * main using gopt to check command line arguments * -h for help */ int main(int argc, char* argv[]) { // Set nabto to default values nabto_main_setup* nms = unabto_init_context(); // Initialise application if (!application_init()){ NABTO_LOG_FATAL(("Unable to initialise serial connection")); } // Optionally set alternative url to html device driver //nmc.nabtoMainSetup.url = "https://dl.dropbox.com/u/15998645/html_dd_demo.zip"; // Overwrite default values with command line args if (!check_args(argc, argv, nms)) { return 1; } NABTO_LOG_INFO(("Identity: '%s'", nms->id)); NABTO_LOG_INFO(("Program Release %" PRIu32 ".%" PRIu32, RELEASE_MAJOR, RELEASE_MINOR)); NABTO_LOG_INFO(("Buffer size: %d" , nms->bufsize)); // Initialize nabto if (!unabto_init()) { NABTO_LOG_FATAL(("Failed at nabto_main_init")); } // The main loop gives nabto a tick from time to time. // Everything else is taken care of behind the scenes. while (true) { unabto_tick(); nabto_yield(10); } unabto_close(); return 0; }
// Set virtual light and return state, // only using ID #1 in this simple example uint8_t setLight(uint8_t id, uint8_t onOff) { theLight = onOff; NABTO_LOG_INFO((theLight?("Nabto: Light turned ON!"):("Nabto: Light turned OFF!"))); // Toggle ACT LED on Raspberry Pi if (theLight) { // Preparing the integer to pass to the thread int *theOrder = malloc(sizeof(*theOrder)); *theOrder = fd; // Send order according to beverage_id iret1 = pthread_create( &thread1, NULL, &sendOrder, theOrder); if(iret1) { // Something went wrong with the thread NABTO_LOG_INFO(("Good old error!\n")); } else { // Succesfully sent the command to the thread. NABTO_LOG_INFO(("Success!\n")); } } else { // Stop clean cycle char stop[] = {133}; write(fd, &stop, sizeof(stop)); usleep ((sizeof(stop)+25) * 100); } return theLight; }
bool unabto_debug_syslog_config(bool enableSyslog, uint8_t facility, uint32_t ip, uint16_t port, uint32_t expire, const uint8_t* configStr, uint16_t configStrLength) { bool ret; nabto_endpoint ep; ep.addr = ip; ep.port = port; NABTO_LOG_INFO(("Enabling syslog " PRIep " %.*s expire %" PRIu32 " enabled %i", MAKE_EP_PRINTABLE(ep), configStrLength, configStr, expire, enableSyslog)); if (enableSyslog) { ret = unabto_log_system_enable_syslog_pattern((const char*)configStr, configStrLength, ip, port, expire); } else { unabto_log_system_disable_syslog(); } return ret; }
static void wifi_callback(uint8_t value) { NABTO_LOG_INFO(("WIFI callback: value = 0x%x", value)); switch(value) { case CONN_STATUS_LINKED: linkIsUp = true; GPIO_ClrBit(LED_LINK_PORT, LED_LINK_PIN); break; case CONN_STATUS_DIS: linkIsUp = false; GPIO_SetBit(LED_LINK_PORT, LED_LINK_PIN); break; } }
// Retrieve the response from a queued request application_event_result application_poll(application_request* request, buffer_read_t* r_b, buffer_write_t* w_b) { application_event_result res; if (saved_app_req == 0) { NABTO_LOG_FATAL(("No queued request")); return AER_REQ_SYSTEM_ERROR; } res = demo_application(request, r_b, w_b); if (res == AER_REQ_RESPONSE_READY) { NABTO_LOG_INFO(("Application poll: Response delivered")); } saved_app_req = 0; return res; }
// Set virtual light and return state, // only using ID #1 in this simple example uint8_t setLight(uint8_t id, uint8_t onOff) { theLight = onOff; NABTO_LOG_INFO((theLight?("Nabto: Light turned ON!"):("Nabto: Light turned OFF!"))); #ifdef __arm__ // Toggle ACT LED on Raspberry Pi if (theLight) { system("echo 1 | sudo tee /sys/class/leds/led0/brightness"); } else { system("echo 0 | sudo tee /sys/class/leds/led0/brightness"); } #endif return theLight; }
// Set virtual light and return state, // only using ID #1 in this simple example uint8_t setLight(uint8_t id, uint8_t onOff) { theLight = onOff; NABTO_LOG_INFO((theLight?("Nabto: Feed pet!"):("Nabto: Dispenser off!"))); #ifdef __arm__ // Toggle servo motor and signaling LED on Raspberry Pi 2 if (theLight) { system("echo | sudo /home/pi/Downloads/unabto_sdk/unabto/demo/pc_demo/src/motor"); } else { system("echo | sudo /home/pi/Downloads/unabto_sdk/unabto/demo/pc_demo/src/shutdown"); } #endif return theLight; }
void modbus_rtu_master_initialize(void) { uint8_t i; modbus* bus; // reset all busses for(i = 0; i < MODBUS_NUMBER_OF_BUSSES; i++) { bus = &busses[i]; bus->identifier = i; bus->uartChannel = i; list_initialize(&bus->messageQueue); bus->state = BUS_STATE_IDLE; } list_initialize(&completedList); NABTO_LOG_INFO(("Initialized.")); }
// Query whether a response to a queued request is ready bool application_poll_query(application_request** appreq) { if (saved_app_req) { /** * Fake a delay for demonstration purpose. * This could be replaced with a variable that is first * set when some proccessing is done and a response * to the client should be send. */ static int fake_delay = 0; nabto_yield(20); if (++fake_delay >= 450) { //3000 fake_delay = 0; *appreq = saved_app_req; NABTO_LOG_INFO(("Application poll query: Response ready")); return true; } } return false; }