add59017818d274b1e7b557de5715e5f27ec4bd0
[sdk/target/sdbd.git] / src / transport_usb.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include <sysdeps.h>
22
23 //#define  TRACE_TAG  TRACE_TRANSPORT
24 #define LOG_TAG "SDBD_TRACE_TRANSPORT"
25 #include "log.h"
26
27 #include "sdb.h"
28
29 #ifdef HAVE_BIG_ENDIAN
30 #define H4(x)   (((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24)
31 static inline void fix_endians(apacket *p)
32 {
33     p->msg.command     = H4(p->msg.command);
34     p->msg.arg0        = H4(p->msg.arg0);
35     p->msg.arg1        = H4(p->msg.arg1);
36     p->msg.data_length = H4(p->msg.data_length);
37     p->msg.data_check  = H4(p->msg.data_check);
38     p->msg.magic       = H4(p->msg.magic);
39 }
40 unsigned host_to_le32(unsigned n)
41 {
42     return H4(n);
43 }
44 #else
45 #define fix_endians(p) do {} while (0)
46 unsigned host_to_le32(unsigned n)
47 {
48     return n;
49 }
50 #endif
51
52 static int remote_read(apacket *p, atransport *t)
53 {
54     if(usb_read(t->usb, &p->msg, sizeof(amessage))){
55         E("remote usb: read terminated (message)\n");
56         return -1;
57     }
58
59     fix_endians(p);
60
61     if(check_header(p, t)) {
62         E("remote usb: check_header failed\n");
63         return -1;
64     }
65
66     if(p->msg.data_length) {
67         if(usb_read(t->usb, p->data, p->msg.data_length)){
68             E("remote usb: terminated (data)\n");
69             return -1;
70         }
71     }
72
73     if(check_data(p)) {
74         E("remote usb: check_data failed\n");
75         return -1;
76     }
77
78     return 0;
79 }
80
81 static int remote_write(apacket *p, atransport *t)
82 {
83     unsigned size = p->msg.data_length;
84
85     fix_endians(p);
86
87     if(usb_write(t->usb, &p->msg, sizeof(amessage))) {
88         E("remote usb: 1 - write terminated\n");
89         return -1;
90     }
91     if(p->msg.data_length == 0) return 0;
92     if(usb_write(t->usb, &p->data, size)) {
93         E("remote usb: 2 - write terminated\n");
94         return -1;
95     }
96
97     return 0;
98 }
99
100 static void remote_close(atransport *t)
101 {
102     usb_close(t->usb);
103     t->usb = 0;
104 }
105
106 static void remote_kick(atransport *t)
107 {
108     usb_kick(t->usb);
109 }
110
111 void init_usb_transport(atransport *t, usb_handle *h, int state)
112 {
113     D("transport: usb\n");
114     t->close = remote_close;
115     t->kick = remote_kick;
116     t->read_from_remote = remote_read;
117     t->write_to_remote = remote_write;
118     t->sync_token = 1;
119     t->connection_state = state;
120     t->type = kTransportUsb;
121     t->usb = h;
122
123     HOST = 0;
124 }