tizen 2.3 release
[framework/connectivity/bluez.git] / android / hal-socket.c
1 /*
2  * Copyright (C) 2013 Intel Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <stdbool.h>
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "hal-ipc.h"
24 #include "hal-log.h"
25 #include "hal-msg.h"
26 #include "hal-utils.h"
27 #include "hal.h"
28
29 static bt_status_t socket_listen(btsock_type_t type, const char *service_name,
30                                         const uint8_t *uuid, int chan,
31                                         int *sock, int flags)
32 {
33         struct hal_cmd_socket_listen cmd;
34
35         if (!sock)
36                 return BT_STATUS_PARM_INVALID;
37
38         DBG("uuid %s chan %d sock %p type %d service_name %s flags 0x%02x",
39                 btuuid2str(uuid), chan, sock, type, service_name, flags);
40
41         memset(&cmd, 0, sizeof(cmd));
42
43         /* type match IPC type */
44         cmd.type = type;
45         cmd.flags = flags;
46         cmd.channel = chan;
47
48         if (uuid)
49                 memcpy(cmd.uuid, uuid, sizeof(cmd.uuid));
50
51         if (service_name)
52                 memcpy(cmd.name, service_name, strlen(service_name));
53
54         return hal_ipc_cmd(HAL_SERVICE_ID_SOCKET, HAL_OP_SOCKET_LISTEN,
55                                 sizeof(cmd), &cmd, NULL, NULL, sock);
56 }
57
58 static bt_status_t socket_connect(const bt_bdaddr_t *bdaddr, btsock_type_t type,
59                                         const uint8_t *uuid, int chan,
60                                         int *sock, int flags)
61 {
62         struct hal_cmd_socket_connect cmd;
63
64         if (!sock)
65                 return BT_STATUS_PARM_INVALID;
66
67         DBG("bdaddr %s uuid %s chan %d sock %p type %d flags 0x%02x",
68                 bdaddr2str(bdaddr), btuuid2str(uuid), chan, sock, type, flags);
69
70         memset(&cmd, 0, sizeof(cmd));
71
72         /* type match IPC type */
73         cmd.type = type;
74         cmd.flags = flags;
75         cmd.channel = chan;
76
77         if (uuid)
78                 memcpy(cmd.uuid, uuid, sizeof(cmd.uuid));
79
80         if (bdaddr)
81                 memcpy(cmd.bdaddr, bdaddr, sizeof(cmd.bdaddr));
82
83         return hal_ipc_cmd(HAL_SERVICE_ID_SOCKET, HAL_OP_SOCKET_CONNECT,
84                                         sizeof(cmd), &cmd, NULL, NULL, sock);
85 }
86
87 static btsock_interface_t socket_if = {
88         sizeof(socket_if),
89         socket_listen,
90         socket_connect
91 };
92
93 btsock_interface_t *bt_get_socket_interface(void)
94 {
95         return &socket_if;
96 }