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