sw_result sw_ipv4_address_init_from_name( sw_ipv4_address * self, sw_const_string addr) { sw_result err = SW_OKAY; // // check to see whether this is dotted decimal or not // int part[4]; int ret; ret = sscanf(addr, "%d.%d.%d.%d", &part[0], &part[1], &part[2], &part[3]); if (ret == 4) { self->m_addr = inet_addr(addr); } else { struct hostent * hEntry = gethostbyname(addr); sw_check(hEntry != NULL, exit, err = SW_E_UNKNOWN); self->m_addr = *(sw_uint32*) hEntry->h_addr; } sw_check(self->m_addr != INADDR_NONE, exit, err = SW_E_UNKNOWN); exit: return err; }
sw_result sw_ipv4_address_init_from_this_host( sw_ipv4_address * self) { #if defined(__VXWORKS__) sw_int8 current_addr[INET_ADDR_LEN]; if (ifAddrGet(sysEnetNameGet(), current_addr) != OK) { sw_debug(SW_LOG_ERROR, "sw_ipv4_address_init_from_this_host", "ifAddrGet() failed\n"); return SW_E_INIT; } self->m_addr = inet_addr(current_addr); #else struct sockaddr_in addr; sw_result err; sw_sockdesc_t desc; sw_socklen_t len; desc = socket(AF_INET, SOCK_DGRAM, 0); sw_check(desc != SW_INVALID_SOCKET, exit, err = SW_E_UNKNOWN); sw_memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = inet_addr("192.168.1.1"); addr.sin_port = htons(5555); err = connect(desc, (struct sockaddr*) &addr, sizeof(addr)); sw_check_okay(err, exit); sw_memset(&addr, 0, sizeof(addr)); len = sizeof(addr); err = getsockname(desc, (struct sockaddr*) &addr, &len); sw_check_okay(err, exit); self->m_addr = addr.sin_addr.s_addr; #endif exit: if (desc != SW_INVALID_SOCKET) { sw_close_socket(desc); } if (err != SW_OKAY) { err = sw_ipv4_address_init_from_address(self, sw_ipv4_address_loopback()); } return err; }
sw_string sw_strdup(sw_const_string str) { sw_string ret = NULL; sw_result err; if (str == NULL) { return NULL; } ret = malloc(strlen(str) + 1); sw_check(ret, exit, err = SW_E_MEM); sw_strcpy(ret, str); exit: return ret; }
int main(){ // Initialize Peripherals interface_init(); red_led_on(); uart_init(BAUDRATE); animation_manager_init(); sys_timer_start(); audio_init(); sei(); // enable global interrupts // Load Default Animation animation_manager_load_animation(START_ANIMATION); // Enter Setup if Requested _delay_ms(100); if(deb_switch_1()){ setup_wb_run(); } else if(deb_switch_2()){ setup_orientation_run(); } // Load Default Animation animation_manager_load_animation(START_ANIMATION); // Set White Balance _delay_ms(300); display_wb_update(); while(uart_async_run()); // setup white balance // Control Panel is Ready => Signal this by Turning the LED Green red_led_off(); green_led_on(); while(1){ // Sleep Mode if(!switch_on_off()){ // if switched off go_to_sleep(); } // Change animations sw_check(); if(sw_check_pressed(SW_LEFT, 200, true)){ animation_manager_dec_animation(); } else if(sw_check_pressed(SW_RIGHT, 200, true)){ animation_manager_inc_animation(); } else if(sw_check_pressed(SW_RAND, 300, true)){ animation_manager_random_animation(); } // Generate Image animation_manager_run(0); // Check Audio audio_start(); while(audio_run()); audio_process(); // Display Image while(uart_async_run()){ interface_async_run(); } } }
sw_result sw_corby_channel_recv( sw_corby_channel self, sw_salt * salt, sw_corby_message * message, sw_uint32 * request_id, sw_string * op, sw_uint32 * op_len, sw_corby_buffer * buffer, sw_uint8 * endian, sw_bool block) { static sw_const_string message_type[] = { "Request", "Reply", "CancelRequest", "LocateRequest", "LocateReply", "CloseConnection", "MessageError" }; sw_size_t buflen; sw_size_t len; sw_result err = SW_OKAY; SW_UNUSED_PARAM(request_id); sw_debug(SW_LOG_VERBOSE, "entering sw_corby_channel_recv()\n"); if (self->m_state == Waiting) { self->m_message->m_header = NULL; } /* * side effect the salt reference */ if (salt) { (*salt) = self->m_orb->m_salt; } while (SW_TRUE) { buflen = (self->m_read_buffer->m_eptr - self->m_read_buffer->m_bptr); *buffer = NULL; if (message) { *message = NULL; } sw_debug(SW_LOG_VERBOSE, " %s %s, buffer(m_base = %x, m_bptr = %x, m_eptr = %x, m_end = %x, buflen = %d)\n", (block) ? "block" : "!block", (self->m_message->m_header) ? "message_header" : "!message_header", self->m_read_buffer->m_base, self->m_read_buffer->m_bptr, self->m_read_buffer->m_eptr, self->m_read_buffer->m_end, buflen); /* beginning a new message */ if (self->m_message->m_header == NULL) { self->m_state = Reading; /* do we need to shift stuff down and reset pointers? */ if (buflen && (self->m_read_buffer->m_bptr != self->m_read_buffer->m_base)) { sw_debug(SW_LOG_VERBOSE, " shifting buffer pointers %d bytes\n", buflen); sw_memcpy(self->m_read_buffer->m_base, self->m_read_buffer->m_bptr, buflen); } self->m_read_buffer->m_bptr = self->m_read_buffer->m_base; self->m_read_buffer->m_eptr = self->m_read_buffer->m_base + buflen; /* do we have enough to read a message header */ if (buflen >= sizeof(struct _sw_swop_message_header)) { self->m_message->m_header = (sw_swop_message_header*) self->m_read_buffer->m_base; sw_debug(SW_LOG_VERBOSE, " SWOP magic = %c %c %c %c\n", self->m_message->m_header->m_magic[0], self->m_message->m_header->m_magic[1], self->m_message->m_header->m_magic[2], self->m_message->m_header->m_magic[3]); /* check magic string */ sw_check ( ((self->m_message->m_header->m_magic[0] == 'S') && (self->m_message->m_header->m_magic[1] == 'W') && (self->m_message->m_header->m_magic[2] == 'O') && (self->m_message->m_header->m_magic[3] == 'P')), exit, err = SW_E_CORBY_BAD_MESSAGE ); sw_debug(SW_LOG_VERBOSE, " SWOP version = %d %d\n", self->m_message->m_header->m_major, self->m_message->m_header->m_minor); /* check version number */ sw_check ( ((self->m_message->m_header->m_major <= SW_SWOP_MAJOR) && (self->m_message->m_header->m_minor <= SW_SWOP_MINOR)), exit, err = SW_E_CORBY_BAD_VERSION ); sw_debug(SW_LOG_VERBOSE, " SWOP endian = %d\n", self->m_message->m_header->m_endian); sw_debug(SW_LOG_VERBOSE, " SWOP message type = %s\n", message_type[self->m_message->m_header->m_msg_type]); /* fix size */ if (self->m_message->m_header->m_endian != SW_ENDIAN) { self->m_message->m_header->m_msg_size = SW_SWAP32(self->m_message->m_header->m_msg_size); } sw_debug(SW_LOG_VERBOSE, " SWOP size = %d\n", self->m_message->m_header->m_msg_size); /* make sure we have enough buffer space */ if (self->m_message->m_header->m_msg_size > ((self->m_read_buffer->m_end - self->m_read_buffer->m_base) - sizeof(struct _sw_swop_message_header))) { sw_uint32 new_size; new_size = self->m_message->m_header->m_msg_size + sizeof(struct _sw_swop_message_header); self->m_read_buffer->m_base = (sw_octets) sw_realloc(self->m_read_buffer->m_base, new_size); sw_check(self->m_read_buffer->m_base, exit, err = SW_E_MEM); self->m_read_buffer->m_bptr = self->m_read_buffer->m_base; self->m_read_buffer->m_eptr = self->m_read_buffer->m_base + buflen; self->m_read_buffer->m_end = self->m_read_buffer->m_base + new_size; self->m_message->m_header = (struct _sw_swop_message_header*) self->m_read_buffer->m_base; } self->m_read_buffer->m_bptr += sizeof(struct _sw_swop_message_header); buflen -= sizeof(struct _sw_swop_message_header); } } /* do we have the whole message? */ if (self->m_message->m_header && (self->m_message->m_header->m_msg_size <= buflen)) { /* * we've read the whole message, so we're now * waiting for a new message */ self->m_state = Waiting; /* side effect endian */ if (endian) { *endian = self->m_message->m_header->m_endian; } sw_corby_channel_did_read(self, self->m_read_buffer->m_base, self->m_message->m_header->m_msg_size + SW_SWOP_HEADER_SIZE); switch (self->m_message->m_header->m_msg_type) { case SW_SWOP_REQUEST: return sw_corby_channel_parse_request(self, message, op, op_len, buffer); case SW_SWOP_REPLY: return sw_corby_channel_parse_reply(self, message, buffer); case SW_SWOP_CANCEL_REQUEST: return sw_corby_channel_parse_cancel_request(self, message, buffer); case SW_SWOP_LOCATE_REQUEST: return sw_corby_channel_parse_locate_request(self, message, buffer); case SW_SWOP_LOCATE_REPLY: return sw_corby_channel_parse_locate_reply(self, message, buffer); case SW_SWOP_CLOSE_CONNECTION: return sw_corby_channel_parse_close_connection(self, message, buffer); default: return sw_corby_channel_message_error(self); } } /* if we get here, then we need to read */ if (block) { err = sw_socket_recvfrom(self->m_socket, self->m_read_buffer->m_eptr, self->m_read_buffer->m_end - self->m_read_buffer->m_eptr, &len, &self->m_from, &self->m_from_port, NULL, NULL); sw_check_okay(err, exit); /* * returning 0 is fair because the socket might have been set up * in non-blocking mode. so if len == 0, we just return from * this function gracefully */ if (len > 0) { self->m_read_buffer->m_eptr += len; buflen = (self->m_read_buffer->m_eptr - self->m_read_buffer->m_bptr); } else { break; } } else { break; } } exit: return err; }
sw_result sw_corby_channel_start_reply( sw_corby_channel self, sw_corby_buffer * buffer, sw_uint32 request_id, sw_corby_reply_status status) { sw_result err; /* * don't do this if this guy isn't expecting a reply */ sw_check(self->m_message->m_body.m_request_header.m_reply_expected, exit, err = SW_E_UNKNOWN); /* blop the SWOP message header */ err = sw_corby_channel_message_header(self, SW_SWOP_REPLY); sw_check_okay(err, exit); /* service context list */ err = sw_corby_buffer_put_uint32(self->m_send_buffer, 0); sw_check_okay(err, exit); /* request id */ err = sw_corby_buffer_put_uint32(self->m_send_buffer, request_id); sw_check_okay(err, exit); /* status */ switch (status) { case SW_CORBY_NO_EXCEPTION: err = sw_corby_buffer_put_uint32(self->m_send_buffer, SW_CORBY_NO_EXCEPTION); sw_check_okay(err, exit); break; case SW_CORBY_SYSTEM_EXCEPTION: err = sw_corby_buffer_put_uint32(self->m_send_buffer, SW_CORBY_SYSTEM_EXCEPTION); sw_check_okay(err, exit); break; case SW_CORBY_USER_EXCEPTION: err = sw_corby_buffer_put_uint32(self->m_send_buffer, SW_CORBY_USER_EXCEPTION); sw_check_okay(err, exit); break; case SW_CORBY_LOCATION_FORWARD: /* unhandled */ break; } *buffer = self->m_send_buffer; exit: if (err != SW_OKAY) { *buffer = NULL; } return err; }