1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 /**
17 * @file sendfile_demo.c
18 *
19 * @brief Provides the sample code for sending a file.
20 *
21 * @since 1.0
22 * @version 1.0
23 */
24
25 // Device A:
26
27 #include <stdio.h>
28 #include "session.h"
29 #include "softbus_config_type.h"
30
31 const char *g_pkgNameA = "dms"; // Application bundle name of device A
32 const char *g_sessionNameA = "ohos.distributedschedule.dms.test"; // Session name of device A
33
34 // Network ID generated by device B after devices A and B are networked
35 const char *g_networkidB = "ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00";
36 const char *g_groupid = "TEST_GROUP_ID"; // Group ID
37 static SessionAttribute g_sessionAttr = {
38 .dataType = TYPE_FILE, // File type
39 };
40 const char *g_rootDir = "/data/"; // Root directory for the files on the receiver side
41 const int FILE_NUM = 4;
42
43 // Notify that the session is set up successfully.
OnSessionOpened(int sessionId,int result)44 static int OnSessionOpened(int sessionId, int result)
45 {
46 printf("session opened,sesison id = %d\r\n", sessionId);
47 return 0;
48 }
49
50 // Notify that the session is closed.
OnSessionClosed(int sessionId)51 static void OnSessionClosed(int sessionId)
52 {
53 printf("session closed, session id = %d\r\n", sessionId);
54 }
55
56 // Session-related callbacks
57 static ISessionListener g_sessionlistenerA = {
58 .OnSessionOpened = OnSessionOpened,
59 .OnSessionClosed = OnSessionClosed,
60 };
61
OnSendFileProcess(int sessionId,uint64_t bytesUpload,uint64_t bytesTotal)62 static int OnSendFileProcess(int sessionId, uint64_t bytesUpload, uint64_t bytesTotal)
63 {
64 printf("OnSendFileProcess sessionId = %d, bytesUpload = %llu, total = %llu\n",
65 sessionId, bytesUpload, bytesTotal);
66 return 0;
67 }
68
OnSendFileFinished(int sessionId,const char * firstFile)69 static int OnSendFileFinished(int sessionId, const char *firstFile)
70 {
71 printf("OnSendFileFinished sessionId = %d, first file = %s\n", sessionId, firstFile);
72 return 0;
73 }
74
OnFileTransError(int sessionId)75 static void OnFileTransError(int sessionId)
76 {
77 printf("OnFileTransError sessionId = %d\n", sessionId);
78 }
79
80 // Callbacks related to file sending
81 static IFileSendListener g_fileSendListener = {
82 .OnSendFileProcess = OnSendFileProcess,
83 .OnSendFileFinished = OnSendFileFinished,
84 .OnFileTransError = OnFileTransError,
85 };
86
main(void)87 int main(void)
88 {
89 /* 1. Device A calls SetFileSendListener() to set callbacks for file sending. */
90 int ret = SetFileSendListener(g_pkgNameA, g_sessionNameA, &g_fileSendListener);
91 printf("set file send listener result = %d\n", ret);
92
93 /*
94 * 2. Device A calls CreateSessionServer() to create a session server based on
95 * the application bundle name and session name, and registers the callbacks for
96 * session opened, session closed, byte received, and message received.
97 */
98 ret = CreateSessionServer(g_pkgNameA, g_sessionNameA, &g_sessionlistenerA);
99 printf("create session server result = %d\n", ret);
100
101 /*
102 * 3. Device A calls OpenSession() to open a session based on the local session name,
103 * peer session name, peer network ID and session type, and determine the session channel based on the session type.
104 * When the session is open, a callback will be invoked to notify devices A and B.
105 * A session ID is returned for subsequent file sending.
106 */
107 int sessionId = OpenSession(g_sessionNameA, g_sessionNameB, g_networkidB, g_groupid, &g_sessionAttr);
108 printf("open session result = %d\n", sessionId);
109
110 /* 4. Device A calls SendFile() to send files to device B. */
111 const char *sfileList[] = {
112 "/data/big.tar",
113 "/data/richu.jpg",
114 "/data/richu-002.jpg",
115 "/data/richu-003.jpg",
116 };
117 ret = SendFile(sessionId, sfileList, NULL, FILE_NUM);
118 printf("send file result = %d\n", ret);
119
120 /* 5. After data transmission is complete, device A calls CloseSession() to close the session
121 * and instructs device B to close the session.
122 */
123 CloseSession(sessionId);
124 printf("SOFTBUS_OK");
125
126 /* 6. After the session is closed, devices A and B call RemoveSessionServer() to remove the session server. */
127 ret = RemoveSessionServer(g_pkgNameA, g_sessionNameA);
128 printf("remove session server result = %d\n", ret);
129 }
130
131 // Device B:
132
133 #include <stdio.h>
134 #include "session.h"
135
136 const char *g_pkgNameB = "dmsB"; // Application bundle name of device B
137 const char *g_sessionNameB = "ohos.distributedschedule.dms.testB"; // Session name of device B
138 const char *g_rootDirB = "/data"; // File directory of device B
139
OnSessionOpened(int sessionId,int result)140 static int OnSessionOpened(int sessionId, int result)
141 {
142 printf("session opened,sesison id = %d\r\n", sessionId);
143 return 0;
144 }
145
OnSessionClosed(int sessionId)146 static void OnSessionClosed(int sessionId)
147 {
148 printf("session closed, session id = %d\r\n", sessionId);
149 }
150
151 static ISessionListener g_sessionlistenerB = {
152 .OnSessionOpened = OnSessionOpened,
153 .OnSessionClosed = OnSessionClosed,
154 };
155
OnReceiveFileStarted(int sessionId,const char * files,int fileCnt)156 static int OnReceiveFileStarted(int sessionId, const char *files, int fileCnt)
157 {
158 printf("File receive start sessionId = %d, first file = %s, fileCnt = %d\n", sessionId, files, fileCnt);
159 return 0;
160 }
161
OnReceiveFileProcess(int sessionId,const char * firstFile,uint64_t bytesUpload,uint64_t bytesTotal)162 static int OnReceiveFileProcess(int sessionId, const char *firstFile, uint64_t bytesUpload, uint64_t bytesTotal)
163 {
164 printf("File receive Process sessionId = %d, first file = %s, bytesUpload = %llu, bytesTotal = %llu\n",
165 sessionId, firstFile, bytesUpload, bytesTotal);
166 return 0;
167 }
168
OnReceiveFileFinished(int sessionId,const char * files,int fileCnt)169 static void OnReceiveFileFinished(int sessionId, const char *files, int fileCnt)
170 {
171 printf("File receive finished sessionId = %d, first file = %s, fileCnt = %d\n", sessionId, files, fileCnt);
172 }
173
OnFileTransError(int sessionId)174 static void OnFileTransError(int sessionId)
175 {
176 printf("OnFileTransError sessionId = %d\n", sessionId);
177 }
178
179 // Callbacks related to file receiving
180 static IFileReceiveListener g_fileRecvListener = {
181 .OnReceiveFileStarted = OnReceiveFileStarted,
182 .OnReceiveFileProcess = OnReceiveFileProcess,
183 .OnReceiveFileFinished = OnReceiveFileFinished,
184 .OnFileTransError = OnFileTransError,
185 };
186
main(void)187 int main(void)
188 {
189 /* 1. Device B calls SetFileReceiveListener() to set callbacks for file receiving. */
190 int ret = SetFileReceiveListener(g_pkgNameB, g_sessionNameB, &g_fileRecvListener, g_rootDirB);
191 printf("set file receive listener result = %d\n", ret);
192
193 /*
194 * 2. Device B calls CreateSessionServer() to create a session server based on
195 * the application bundle name and session name, and registers the callbacks for
196 * session opened, session closed, byte received, and message received.
197 */
198 ret = CreateSessionServer(g_pkgNameB, g_sessionNameB, &g_sessionlistenerB);
199 printf("create session server result = %d\n", ret);
200
201 /*
202 * 3. Upon receiving the session open notification via OnSessionOpened(), device B waits for device A to send files.
203 * When receiving the files, device B returns the file receiving status via
204 * OnReceiveFileProcess(), OnReceiveFileStarted(), and OnReceiveFileFinished().
205 */
206
207 /* 4. When all the files are received, device B closes the session and removes the session service. */
208 ret = RemoveSessionServer(g_pkgNameB, g_sessionNameB);
209 printf("remove session server result = %d\n", ret);
210 }
211