void camera_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset) { const char *temp; uint8_t buff[1024]; camera_size_t camera_size = CAMERA_SIZE_1; u16_t count; u32_t size; int index = 0, len = 0, tries = 10; if (0 == *offset) { /* Determine the size of picture. 1 for the smallest and 3 for the largest. */ if (!REST.get_query_variable(request, "size", &temp)) { REST.get_post_variable(request, "size", &temp); } if (temp) { camera_size = camera_parse_size(temp[0]); } leds_toggle(LEDS_GREEN); camera_take_picture(camera_size, preferred_size, &size, &count); } index = *offset / preferred_size + 1; count = camera_get_packet_count(); len = camera_try_get_packet(index, buff, tries); *offset += len; if (index >= count) { *offset = -1; } REST.set_header_content_type(response, REST.type.IMAGE_JPEG); REST.set_response_payload(response, buff, len); }
static void udp_handler(void) { static uint8_t tries = 10; static camera_size_t camera_size; static uint16_t index, count, len; static uint32_t size; char temp[MAX_PAYLOAD_LEN]; if (uip_newdata()) { uip_ipaddr_copy(&server_conn->ripaddr, &UDP_HDR->srcipaddr); server_conn->rport = UDP_HDR->srcport; if (1 == uip_datalen()) { camera_size = camera_parse_size(((char*)uip_appdata)[0]); camera_take_picture(camera_size, DEFAULT_CAMERA_PACKET_SIZE, &size, &count); /* Send picture size and packet count. */ /* The first 2 bytes are packet_no. */ /* Here set packet_no to 0 as this packet is not a real packet. */ temp[0] = temp[1] = 0; temp[2] = size; /* The next 4 bytes are size. */ temp[3] = size >> 8; temp[4] = size >> 16; temp[5] = size >> 24; /* The coming is count. */ temp[6] = count; temp[7] = count >> 8; uip_udp_packet_send(server_conn, temp, 8); /* Send picture packet by packet. */ for (index = 1; index <= count; index++) { len = camera_try_get_packet(index, buffer, tries); /* The first 2 bytes are packet_no. */ temp[0] = index; temp[1] = index >> 8; /* Copy the picture data next to packet_no. */ memcpy(temp + 2, buffer, len); uip_udp_packet_send(server_conn, temp, len + 2); } } else {
/* * Take a picture. */ void take_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset) { const char *temp; char buf[64] = ""; camera_size_t camera_size = CAMERA_SIZE_1; uint16_t count; uint32_t size; /* Determine the size of picture. 1 for the smallest and 3 for the largest. */ if (!REST.get_query_variable(request, "size", &temp)) { REST.get_post_variable(request, "size", &temp); } if (temp) { camera_size = camera_parse_size(temp[0]); } camera_take_picture(camera_size, DEFAULT_CAMERA_PACKET_SIZE, &size, &count); sprintf(buf, "{'t':'OK','s':%lu,'n':%u}", size, count); REST.set_header_content_type(response, TEXT_PLAIN); REST.set_response_payload(response, buf, strlen(buf)); }