Created
February 24, 2013 20:11
-
-
Save modeswitch/5025386 to your computer and use it in GitHub Desktop.
scan_example (ported)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <errno.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
#include "cmd_def.h" | |
volatile int serial_fd; | |
void output(uint8 len1, uint8* data1, uint16 len2, uint8* data2) | |
{ | |
int written; | |
written = write(serial_fd, data1, len1); | |
if(written < 0) { | |
fprintf(stderr, "ERROR: Writing data. %d\n", (int)errno); | |
exit(-1); | |
} | |
printf("WRITE1: %d bytes\n", written); | |
written = write(serial_fd, data2, len2); | |
if(written < 0) { | |
fprintf(stderr, "ERROR: Writing data. %d\n", (int)errno); | |
exit(-1); | |
} | |
printf("WRITE2: %d bytes\n", written); | |
} | |
int read_message() | |
{ | |
int rread; | |
const struct ble_msg *apimsg; | |
struct ble_header apihdr; | |
unsigned char data[256];//enough for BLE | |
//read header | |
rread = read(serial_fd, (unsigned char*)&apihdr, 4); | |
if(rread < 0) { | |
return errno; | |
} else if(!rread) { | |
return 0; | |
} | |
printf("READ: header %d bytes\n", rread); | |
//read rest if needed | |
if(apihdr.lolen) | |
{ | |
rread = read(serial_fd, data, apihdr.lolen); | |
if(rread < 0) { | |
return errno; | |
} | |
printf("READ: data %d bytes", rread); | |
} | |
apimsg=ble_get_msg_hdr(apihdr); | |
if(!apimsg) | |
{ | |
fprintf(stderr, "ERROR: Message not found:%d:%d\n",(int)apihdr.cls,(int)apihdr.command); | |
return -1; | |
} | |
apimsg->handler(data); | |
return 0; | |
} | |
void print_help() | |
{ | |
printf("Demo application to scan devices\n"); | |
printf("\tscan_example\tdevice\n"); | |
} | |
int main(int argc, char *argv[] ) | |
{ | |
char* portname; | |
if(argc<2) | |
{ | |
print_help(); | |
exit(-1); | |
} | |
portname = argv[1]; | |
serial_fd = open(portname, O_RDWR | O_SYNC); | |
if (serial_fd < 0) | |
{ | |
fprintf (stderr, "error %d opening %s: %s", errno, portname, strerror (errno)); | |
return -1; | |
} | |
bglib_output = output; | |
//stop previous operation | |
ble_cmd_gap_end_procedure(); | |
//get connection status,current command will be handled in response | |
ble_cmd_connection_get_status(0); | |
//Message loop | |
while(1) | |
{ | |
if(read_message()) | |
{ | |
fprintf(stderr, "Error reading message\n"); | |
break; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment