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 dfs_demo.c
18  *
19  * @brief Provides the sample code for obtaining session information.
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_def.h"
30 #include "softbus_config_type.h"
31 
32 const char *g_pkgNameA = "dms"; // Application bundle name of device A
33 
34 // Session name of the distributed file service of device A
35 const char *g_sessionNameA = "ohos.distributedschedule.dms.test";
36 
37 // Session name of the distributed file service of device B
38 const char *g_sessionNameB = "ohos.distributedschedule.dms.test";
39 
40 // Network ID generated by device B after devices A and B are networked
41 const char *g_networkidB = "ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00";
42 const char *g_groupid = "TEST_GROUP_ID";  // Group ID
43 static SessionAttribute g_sessionAttr = {
44     .dataType = TYPE_BYTES,  // Session type
45 };
46 const char *g_testData = "TranSessionTest_GetSessionKeyTestData";
47 
48 // Notify that the session is set up successfully.
OnSessionOpened(int sessionId,int result)49 static int OnSessionOpened(int sessionId, int result)
50 {
51     printf("session opened,sesison id = %d\r\n", sessionId);
52     return 0;
53 }
54 
55 // Notify that the session is closed.
OnSessionClosed(int sessionId)56 static void OnSessionClosed(int sessionId)
57 {
58     printf("session closed, session id = %d\r\n", sessionId);
59 }
60 
61 // Notify that the byte data is received.
OnBytesReceived(int sessionId,const void * data,unsigned int len)62 static void OnBytesReceived(int sessionId, const void *data, unsigned int len)
63 {
64     printf("session bytes received, session id = %d\r\n", sessionId);
65 }
66 
67 // Notify that the message is received.
OnMessageReceived(int sessionId,const void * data,unsigned int len)68 static void OnMessageReceived(int sessionId, const void *data, unsigned int len)
69 {
70     printf("session msg received, session id = %d\r\n", sessionId);
71 }
72 
73 // Notify that the stream data is received.
OnStreamReceived(int sessionId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param)74 static void OnStreamReceived(int sessionId, const StreamData *data, const StreamData *ext,
75                              const StreamFrameInfo *param)
76 {
77     printf("session stream received, session id = %d\r\n", sessionId);
78 }
79 
80 static ISessionListener g_sessionlistenerA = {
81     .OnSessionOpened = OnSessionOpened,
82     .OnSessionClosed = OnSessionClosed,
83     .OnBytesReceived = OnBytesReceived,
84     .OnMessageReceived = OnMessageReceived,
85     .OnStreamReceived = OnStreamReceived,
86 };
87 
main(void)88 int main(void)
89 {
90     /*
91      * 1. Device A calls CreateSessionServer() to create a session server based on
92      * the application bundle name and session name, and registers the callbacks for
93      * session opened, session closed, byte received, and message received.
94      */
95     int ret = CreateSessionServer(g_pkgNameA, g_sessionNameA, &g_sessionlistenerA);
96     printf("create session server result = %d\n", ret);
97 
98     /*
99      * 2. Device A calls OpenSession() to open a session based on the local session name,
100      * peer session name, and peer network ID and determine the session channel based on the session type.
101      * When the session is open, a callback will be invoked to notify devices A and B.
102      * A session ID is returned for subsequent operations.
103      */
104     int sessionId = OpenSession(g_sessionNameA, g_sessionNameB, g_networkidB, g_groupid, &g_sessionAttr);
105     printf("open session result = %d\n", sessionId);
106 
107     /* 3. Device A obtains session information. */
108     char sessionName[SESSION_NAME_SIZE_MAX] = {0};
109     ret = GetMySessionName(sessionId, sessionName, SESSION_NAME_SIZE_MAX);
110     printf("get mySessionName result = %d\n", ret);
111 
112     char deviceId[DEVICE_ID_SIZE_MAX] = {0};
113     ret = GetPeerDeviceId(sessionId, deviceId, DEVICE_ID_SIZE_MAX);
114     printf("get peerDeviceId result = %d\n", ret);
115 
116     char peerSessionName[SESSION_NAME_SIZE_MAX] = {0};
117     ret = GetPeerSessionName(sessionId, peerSessionName, SESSION_NAME_SIZE_MAX);
118     printf("get peerSessionName result = %d\n", ret);
119 
120     ret = GetSessionSide(sessionId);
121     printf("get sessionside result = %d\n", ret);
122 }
123