ArDrone SDK 1.8 added
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / ARDroneLib / VP_SDK / Examples / common / com_server.c
1 // ----------------------------------------------
2 //
3 //  Author : <sylvain.gaeremynck\@parrot.fr>
4 //  Date   : 15/01/2007
5 //
6 //  Parrot Video SDK : Examples/common
7 //
8 // ---------------------------------------------- 
9
10 #include "common.h"
11
12 #include <Com/com.h>
13 #include <Api/error.h>
14 #include <VP_Os/vp_os_print.h>
15
16 #include "com_server.h"
17
18 static com_config_t config;
19 static com_socket_t srv;
20 static com_socket_t clt;
21 static Read         read;
22 static Write        write;
23
24 C_RESULT init_com_server(void)
25 {
26   config.connection       = COM_BLUETOOTH;
27   config.localAdapterName = DEVICENAME;
28   config.localIpAddress   = SERVERHOST;
29   config.localIpSubmask   = SUBMASK;
30
31   if(FAILED(com_init(&config)))
32     return C_FAIL;
33
34   return C_OK;
35 }
36
37 C_RESULT run_com_server(COM_PROTOCOL protocol)
38 {
39   srv.socket    = COM_SERVER;
40   srv.protocol  = protocol;
41
42   if(protocol == COM_RFCOMM)
43   {
44     srv.scn = BTADDR_SCN;
45   }
46   else if(protocol == COM_BNEP)
47   {
48     srv.port        = BTADDR_PORT;
49     srv.serverHost  = SERVERHOST;
50   }
51   else
52     return C_FAIL;
53
54   if(FAILED(com_open(&srv,&read,&write)))
55     return C_FAIL;
56
57   com_passKey(PIN_CODE);
58   if(FAILED(com_accept(&srv,&clt)))
59     return C_FAIL;
60
61   return C_OK;
62 }
63
64 C_RESULT shutdown_com_server(void)
65 {
66   com_close(&clt);
67   com_close(&srv);
68
69   com_shutdown();
70
71   return C_OK;
72 }
73
74 C_RESULT read_server(int8_t* buffer, int32_t* size)
75 {
76   return read(&clt,buffer,size);
77 }
78
79 C_RESULT write_server(const int8_t* buffer, int32_t* size)
80 {
81   return write(&clt,buffer,size);
82 }