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 sendbytes_message_demo.c
18 *
19 * @brief Provides the sample code code for sending messages or byte data.
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_BYTES, // Session type
39 };
40
41 // Notify that the session is set up successfully.
OnSessionOpened(int sessionId,int result)42 static int OnSessionOpened(int sessionId, int result)
43 {
44 printf("session opened,sesison id = %d\r\n", sessionId);
45 return 0;
46 }
47
48 // Notify that the session is closed.
OnSessionClosed(int sessionId)49 static void OnSessionClosed(int sessionId)
50 {
51 printf("session closed, session id = %d\r\n", sessionId);
52 }
53
54 // Notify that the byte data is received.
OnBytesReceived(int sessionId,const void * data,unsigned int len)55 static void OnBytesReceived(int sessionId, const void *data, unsigned int len)
56 {
57 printf("session bytes received, session id = %d\r\n", sessionId);
58 }
59
60 // Notify that the message is received.
OnMessageReceived(int sessionId,const void * data,unsigned int len)61 static void OnMessageReceived(int sessionId, const void *data, unsigned int len)
62 {
63 printf("session msg received, session id = %d\r\n", sessionId);
64 }
65
66 static ISessionListener g_sessionlistenerA = {
67 .OnSessionOpened = OnSessionOpened,
68 .OnSessionClosed = OnSessionClosed,
69 .OnBytesReceived = OnBytesReceived,
70 .OnMessageReceived = OnMessageReceived,
71 };
72
main(void)73 int main(void)
74 {
75 /*
76 * 1. Device A calls CreateSessionServer() to create a session server based on the application bundle name and
77 * session name, and registers the callbacks for session opened, session closed, byte received, and message
78 * received.
79 */
80 int ret = CreateSessionServer(g_pkgNameA, g_sessionNameA, &g_sessionlistenerA);
81 printf("create session server result = %d\n", ret);
82
83 /*
84 * 2. Device A calls OpenSession() to open a session based on the local session name,
85 * peer session name, and peer network ID and determine the session channel based on the session type.
86 * When the session is opened, a callback will be invoked to notify devices A and B.
87 * A session ID is returned for subsequent data sending.
88 */
89 int sessionId = OpenSession(g_sessionNameA, g_sessionNameB, g_networkidB, g_groupid, &g_sessionAttr);
90 printf("open session result = %d\n", sessionId);
91
92 /* 3. Device A calls SendBytes() to send byte data or calls SendMessage() to send messages to device B. */
93 const char *data = "testdata";
94 uint32_t len = strlen(data);
95 ret = SendBytes(sessionId, data, len);
96 printf("send bytes result = %d\n", ret);
97
98 ret = SendMessage(sessionId, data, len);
99 printf("send message result = %d\n", ret);
100
101 /* 4. After data transmission is complete, device A calls CloseSession() to close the session
102 * and instructs device B to close the session.
103 */
104 CloseSession(sessionId);
105 printf("SOFTBUS_OK");
106
107 /* 5. After the session is closed, devices A and B call RemoveSessionServer() to remove the session server. */
108 ret = RemoveSessionServer(g_pkgNameA, g_sessionNameA);
109 printf("remove session server result = %d\n", ret);
110 }
111
112 // Device B:
113
114 #include <stdio.h>
115 #include "session.h"
116
117 const char *g_pkgNameB = "dmsB"; // Application bundle name of device B
118 const char *g_sessionNameB = "ohos.distributedschedule.dms.testB"; // Session name of device B
119
OnSessionOpened(int sessionId,int result)120 static int OnSessionOpened(int sessionId, int result)
121 {
122 printf("session opened,sesison id = %d\r\n", sessionId);
123 return 0;
124 }
125
OnSessionClosed(int sessionId)126 static void OnSessionClosed(int sessionId)
127 {
128 printf("session closed, session id = %d\r\n", sessionId);
129 }
130
OnBytesReceived(int sessionId,const void * data,unsigned int len)131 static void OnBytesReceived(int sessionId, const void *data, unsigned int len)
132 {
133 printf("session bytes received, session id = %d\r\n", sessionId);
134 }
135
OnMessageReceived(int sessionId,const void * data,unsigned int len)136 static void OnMessageReceived(int sessionId, const void *data, unsigned int len)
137 {
138 printf("session msg received, session id = %d\r\n", sessionId);
139 }
140
141 static ISessionListener g_sessionlistenerB = {
142 .OnSessionOpened = OnSessionOpened,
143 .OnSessionClosed = OnSessionClosed,
144 .OnBytesReceived = OnBytesReceived,
145 .OnMessageReceived = OnMessageReceived,
146 };
147
main(void)148 int main(void)
149 {
150 /*
151 * 1. Device B calls CreateSessionServer() to create a session server based on
152 * the application bundle name and session name, and registers the callbacks for
153 * session opened, session closed, byte received, and message received.
154 */
155 int ret = CreateSessionServer(g_pkgNameB, g_sessionNameB, &g_sessionlistenerB);
156 printf("create session server result = %d\n", ret);
157
158 /*
159 * 2. Upon receiving the session open notification via OnSessionOpened(), device B waits for device A to send data.
160 * When receiving data, device B returns the receiving status via OnBytesReceived() or OnMessageReceived().
161 */
162
163 /* 3. When the data is received, device B closes the session and removes the session server. */
164 ret = RemoveSessionServer(g_pkgNameB, g_sessionNameB);
165 printf("remove session server result = %d\n", ret);
166 }
167