• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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  * @addtogroup Bluetooth
18  * @{
19  *
20  * @brief Defines map client stm object.
21  *
22  */
23 
24 /**
25  * @file map_mce_instance_stm.cpp
26  *
27  * @brief map instance client stm source file .
28  *
29  */
30 
31 #include "map_mce_instance_stm.h"
32 #include "log.h"
33 #include "map_mce_mns_server.h"
34 
35 namespace OHOS {
36 namespace bluetooth {
CreateStm()37 void MapMceInstanceStm::CreateStm()
38 {
39     btDevice_ = mceDeviceMgr_.GetBtDevice();
40     btAddress_ = mceDeviceMgr_.GetBtAddress();
41 
42     // create mas client
43     MapMceObserverManager &observer = mceDeviceMgr_.GetDeviceCallbackMgr();
44     masClient_ = std::make_unique<MapMceInstanceClient>(
45         *this, mceStmDispacher_, observer, insDefaultConfig_, mceInstanceStmMutex_);
46     // main state create
47     std::unique_ptr<State> disconnectedState = std::make_unique<MapMceDisconnectedState>(*this);
48     std::unique_ptr<State> connectingState = std::make_unique<MapMceConnectingState>(*this);
49     std::unique_ptr<State> connectedState = std::make_unique<MapMceConnectedState>(*this);
50     std::unique_ptr<State> disconnectingState = std::make_unique<MapMceDisconnectingState>(*this);
51     // connected sub state
52     std::unique_ptr<State> sendingState = std::make_unique<MapMceConnectedStateSubReqSending>(*this, *connectedState);
53 
54     // add main state
55     Move(disconnectedState);
56     Move(connectingState);
57     Move(connectedState);
58     Move(disconnectingState);
59     // add connected sub state
60     Move(sendingState);
61     // init status
62     InitState(MCE_DISCONNECTED_STATE);
63 }
64 
MapMceInstanceStm(MapMceDeviceCtrl & deviceMgr,utility::Dispatcher & dispatcher,int instanceId,const MasInstanceConfig & config)65 MapMceInstanceStm::MapMceInstanceStm(
66     MapMceDeviceCtrl &deviceMgr, utility::Dispatcher &dispatcher, int instanceId, const MasInstanceConfig &config)
67     : mceDeviceMgr_(deviceMgr), mceStmDispacher_(dispatcher)
68 {
69     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
70     masInstanceId_ = instanceId;
71     targetState_ = MCE_INSTANCE_STATE_DISCONNECTED;
72     masClient_ = nullptr;
73     insDefaultConfig_ = config;
74     stmSendFlag = false;
75     stmDataContinueFlag_ = false;
76     stmDataStmBusyFlag_ = false;
77     busyFlag_ = false;
78     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
79 };
80 
~MapMceInstanceStm()81 MapMceInstanceStm::~MapMceInstanceStm()
82 {
83     LOG_INFO("%{public}s execute", __PRETTY_FUNCTION__);
84 }
85 
PostMessage(utility::Message msg)86 void MapMceInstanceStm::PostMessage(utility::Message msg)
87 {
88     LOG_INFO("%{public}s execute, msg=0x%x", __PRETTY_FUNCTION__, msg.what_);
89     mceStmDispacher_.PostTask(std::bind(&MapMceInstanceStm::MceProcessMessage, this, msg));
90 }
91 
PostMessageWithRequest(utility::Message msg,std::unique_ptr<MapMceInstanceRequest> & req)92 void MapMceInstanceStm::PostMessageWithRequest(utility::Message msg, std::unique_ptr<MapMceInstanceRequest> &req)
93 {
94     LOG_INFO("%{public}s enter, msg=0x%x", __PRETTY_FUNCTION__, msg.what_);
95     std::lock_guard<std::recursive_mutex> lock(mceInstanceStmMutex_);
96 
97     if (!stmSendFlag) {
98         stmSendFlag = true;
99         stmRequestPtr_ = std::move(req);
100         mceStmDispacher_.PostTask(std::bind(&MapMceInstanceStm::MceProcessMessage, this, msg));
101     } else {
102         masClient_->ClientSaveRequest(req);
103     }
104     req = nullptr;
105     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
106 }
107 
MceProcessMessageWithRequest(utility::Message msg,std::unique_ptr<MapMceInstanceRequest> & req)108 void MapMceInstanceStm::MceProcessMessageWithRequest(utility::Message msg, std::unique_ptr<MapMceInstanceRequest> &req)
109 {
110     LOG_INFO("%{public}s enter, msg=0x%x", __PRETTY_FUNCTION__, msg.what_);
111     std::lock_guard<std::recursive_mutex> lock(mceInstanceStmMutex_);
112 
113     MceProcessMessageWithRequestInternal(msg, req);
114     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
115 }
116 
MceProcessMessageWithRequestInternal(utility::Message msg,std::unique_ptr<MapMceInstanceRequest> & req)117 void MapMceInstanceStm::MceProcessMessageWithRequestInternal(
118     utility::Message msg, std::unique_ptr<MapMceInstanceRequest> &req)
119 {
120     LOG_INFO("%{public}s execute, msg=0x%x", __PRETTY_FUNCTION__, msg.what_);
121     if (!stmSendFlag) {
122         stmSendFlag = true;
123         stmRequestPtr_ = std::move(req);
124         ProcessMessage(msg);
125         if ((msg.what_ == MSG_MASSTM_REQ_SEND_REQUEST_SELF) || (msg.what_ == MSG_MASSTM_REQ_SEND_REQUEST)) {
126             stmSendFlag = false;
127         }
128     } else {
129         masClient_->ClientSaveRequest(req);
130     }
131     req = nullptr;
132 }
133 
GetBusyStatus()134 bool MapMceInstanceStm::GetBusyStatus()
135 {
136     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
137     return busyFlag_;
138 }
139 
ChangeContinueBusyStatus(bool status)140 void MapMceInstanceStm::ChangeContinueBusyStatus(bool status)
141 {
142     LOG_INFO("%{public}s execute", __PRETTY_FUNCTION__);
143     bool current = false;
144 
145     stmDataContinueFlag_ = status;
146     if (stmDataContinueFlag_ || stmDataStmBusyFlag_) {
147         current = true;
148     }
149     if (busyFlag_ != current) {
150         busyFlag_ = current;
151         // send to dev ctrl
152         utility::Message outMsg(MSG_MCEDEVICE_INSCLIENT_INFO_BUSY_STATUS_CHANGE);
153         outMsg.arg1_ = int(busyFlag_);
154         mceDeviceMgr_.PostMessage(outMsg);
155     }
156     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
157 }
158 
ChangeStmBusyStatus(bool status)159 void MapMceInstanceStm::ChangeStmBusyStatus(bool status)
160 {
161     LOG_INFO("%{public}s execute", __PRETTY_FUNCTION__);
162     bool current = false;
163 
164     stmDataStmBusyFlag_ = status;
165     if (stmDataContinueFlag_ || stmDataStmBusyFlag_) {
166         current = true;
167     }
168     if (busyFlag_ != current) {
169         busyFlag_ = current;
170         // send to dev ctrl
171         utility::Message outMsg(MSG_MCEDEVICE_INSCLIENT_INFO_BUSY_STATUS_CHANGE);
172         outMsg.arg1_ = int(busyFlag_);
173         mceDeviceMgr_.PostMessage(outMsg);
174     }
175 }
176 
GetMasClient()177 MapMceInstanceClient &MapMceInstanceStm::GetMasClient()
178 {
179     return *masClient_;
180 }
181 
PostMessageWithObexHeader(utility::Message msg,ObexHeader head)182 void MapMceInstanceStm::PostMessageWithObexHeader(utility::Message msg, ObexHeader head)
183 {
184     LOG_INFO("%{public}s execute, msg=0x%x", __PRETTY_FUNCTION__, msg.what_);
185     mceStmDispacher_.PostTask(std::bind(&MapMceInstanceStm::MceProcessMessageWithObexHeader, this, msg, head));
186 }
187 
MceProcessMessageWithObexHeader(utility::Message msg,ObexHeader head)188 void MapMceInstanceStm::MceProcessMessageWithObexHeader(utility::Message msg, ObexHeader head)
189 {
190     stmObexHeader_ = std::make_unique<ObexHeader>(head);
191     MceProcessMessage(msg);
192 }
193 
MceProcessMessage(utility::Message msg)194 void MapMceInstanceStm::MceProcessMessage(utility::Message msg)
195 {
196     LOG_INFO("%{public}s enter,msg=0x%x", __PRETTY_FUNCTION__, msg.what_);
197     std::lock_guard<std::recursive_mutex> lock(mceInstanceStmMutex_);
198 
199     ProcessMessage(msg);
200 
201     switch (msg.what_) {
202         case MSG_MASSTM_REQ_SEND_REQUEST_SELF:
203         case MSG_MASSTM_REQ_SEND_REQUEST:
204             stmSendFlag = false;
205             break;
206         case MSG_MASSTM_OBEX_INFO_CHG_CONTINUEBUSY_STATUS:
207             ChangeContinueBusyStatus((bool)msg.arg1_);
208             break;
209         default:
210             break;
211     }
212     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
213 }
214 
GetTargetState()215 MceInstanceStateType MapMceInstanceStm::GetTargetState()
216 {
217     return targetState_;
218 }
219 
GetCurrentMceStmStateName()220 std::string MapMceInstanceStm::GetCurrentMceStmStateName()
221 {
222     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
223     std::lock_guard<std::recursive_mutex> lock(mceInstanceStmMutex_);
224 
225     std::string state = GetState()->Name();
226     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
227     return state;
228 }
229 
GetBtDevice()230 std::string MapMceInstanceStm::GetBtDevice()
231 {
232     return btDevice_;
233 }
234 
GetBtAddress()235 BtAddr MapMceInstanceStm::GetBtAddress()
236 {
237     return btAddress_;
238 }
239 
TansTargetState(MceInstanceStateType state)240 void MapMceInstanceStm::TansTargetState(MceInstanceStateType state)
241 {
242     LOG_INFO("%{public}s execute, state=%{public}d", __PRETTY_FUNCTION__, state);
243     targetState_ = state;
244 }
245 
246 // MCE_DISCONNECTED_STATE
Entry()247 void MapMceInstanceStm::MapMceDisconnectedState::Entry()
248 {
249     LOG_INFO("%{public}s execute", __PRETTY_FUNCTION__);
250 }
251 
Exit()252 void MapMceInstanceStm::MapMceDisconnectedState::Exit()
253 {
254     LOG_INFO("%{public}s execute", __PRETTY_FUNCTION__);
255 }
256 
Dispatch(const utility::Message & msg)257 bool MapMceInstanceStm::MapMceDisconnectedState::Dispatch(const utility::Message &msg)
258 {
259     LOG_INFO("%{public}s enter, msg=0x%x", __PRETTY_FUNCTION__, msg.what_);
260     bool ret = true;
261     switch (msg.what_) {
262         case MSG_MASSTM_REQ_CONNECT:
263             mceInstanceStm_.TansTargetState(MCE_INSTANCE_STATE_CONNECTED);
264             Transition(MCE_CONNECTING_STATE);
265             break;
266         case MSG_MASSTM_REQ_DISCONNECT:
267             // set target status disconnect
268             mceInstanceStm_.TansTargetState(MCE_INSTANCE_STATE_DISCONNECTED);
269             break;
270         default:
271             ret = false;
272             break;
273     }
274     LOG_INFO("%{public}s exit", __PRETTY_FUNCTION__);
275     return ret;
276 }
277 
278 // MCE_DISCONNECTING_STATE
Entry()279 void MapMceInstanceStm::MapMceDisconnectingState::Entry()
280 {
281     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
282 
283     // obex client execute disconnect
284     if (mceInstanceStm_.masClient_ != nullptr) {
285         int retVal = mceInstanceStm_.masClient_->StartClientDisConnect();
286         if (retVal != BT_NO_ERROR) {
287             // error occur
288             // post msg to mns state machine instance. execute in mns service thread
289             utility::Message outMsg = MSG_MASSTM_OBEX_DISCONNECTED;
290             mceInstanceStm_.PostMessage(outMsg);
291             LOG_INFO("%{public}s start obex error", __PRETTY_FUNCTION__);
292         }
293     }
294     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
295 }
296 
Exit()297 void MapMceInstanceStm::MapMceDisconnectingState::Exit()
298 {
299     LOG_INFO("%{public}s execute", __PRETTY_FUNCTION__);
300 }
301 
Dispatch(const utility::Message & msg)302 bool MapMceInstanceStm::MapMceDisconnectingState::Dispatch(const utility::Message &msg)
303 {
304     LOG_INFO("%{public}s enter, msg=0x%x", __PRETTY_FUNCTION__, msg.what_);
305     bool ret = true;
306     utility::Message outMsg(MSG_MCEDEVICE_INSCLIENT_TRANSPORT_ERRO);
307     switch (msg.what_) {
308         case MSG_MASSTM_OBEX_TRANSPORT_FAILED:
309             Transition(MCE_DISCONNECTED_STATE);
310             mceInstanceStm_.TansTargetState(MCE_INSTANCE_STATE_DISCONNECTED);
311             mceInstanceStm_.masClient_->ProcessDisConnectFinish();
312             // post message to instance stm
313             outMsg.what_ = MSG_MCEDEVICE_INSCLIENT_TRANSPORT_ERRO;
314             mceInstanceStm_.mceDeviceMgr_.PostMessage(outMsg);
315             break;
316         case MSG_MASSTM_OBEX_DISCONNECTED:
317             // disconnected finish
318             mceInstanceStm_.masClient_->ProcessDisConnectFinish();
319             // send disconnected to device mgr
320             outMsg.what_ = MSG_MCEDEVICE_INSCLIENT_DISCONNECTED;
321             mceInstanceStm_.mceDeviceMgr_.PostMessage(outMsg);
322             // check target state
323             if (mceInstanceStm_.GetTargetState() == MCE_INSTANCE_STATE_CONNECTED) {
324                 // connecting again
325                 Transition(MCE_CONNECTING_STATE);
326             } else {
327                 // change to disconnected
328                 Transition(MCE_DISCONNECTED_STATE);
329             }
330             break;
331         case MSG_MASSTM_REQ_CONNECT:
332             // set target mode connect
333             mceInstanceStm_.TansTargetState(MCE_INSTANCE_STATE_CONNECTED);
334             break;
335         case MSG_MASSTM_REQ_DISCONNECT:
336             // set target mode disconnect
337             mceInstanceStm_.TansTargetState(MCE_INSTANCE_STATE_DISCONNECTED);
338             break;
339         default:
340             ret = false;
341             // default ret
342             break;
343     }
344     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
345     return ret;
346 }
347 
348 // MCE_CONNECTING_STATE
Entry()349 void MapMceInstanceStm::MapMceConnectingState::Entry()
350 {
351     LOG_INFO("%{public}s execute", __PRETTY_FUNCTION__);
352     // start connecting.
353     if (mceInstanceStm_.masClient_->StartClientConnect() != BT_NO_ERROR) {
354         // error process
355         utility::Message outMsg(MSG_MASSTM_OBEX_DISCONNECTED);
356         mceInstanceStm_.PostMessage(outMsg);
357         LOG_ERROR("%{public}s StartClientConnect execute error", __PRETTY_FUNCTION__);
358     }
359 }
360 
Exit()361 void MapMceInstanceStm::MapMceConnectingState::Exit()
362 {
363     LOG_INFO("%{public}s execute", __PRETTY_FUNCTION__);
364 }
365 
DispatchProcObexConnected()366 void MapMceInstanceStm::MapMceConnectingState::DispatchProcObexConnected()
367 {
368     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
369     // check target state
370     if (mceInstanceStm_.GetTargetState() == MCE_INSTANCE_STATE_DISCONNECTED) {
371         // start disconnecting
372         Transition(MCE_DISCONNECTING_STATE);
373     } else {
374         // change to connected state
375         Transition(MCE_CONNECTED_STATE);
376         mceInstanceStm_.masClient_->ClientSendReqGetMasInstanceInformation();
377         std::u16string setpathPaths = u"";
378         std::vector<std::u16string> setpathPathList;
379         setpathPathList.push_back(MCE_FOLDER_TELECOM);
380         setpathPathList.push_back(MCE_FOLDER_MSG);
381         mceInstanceStm_.masClient_->ClientSendReqSetPath(int(MASSTM_SETPATH_LIST), setpathPaths, setpathPathList);
382     }
383 }
384 
DispatchProcObexDisconnected()385 void MapMceInstanceStm::MapMceConnectingState::DispatchProcObexDisconnected()
386 {
387     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
388     // receive the obex disconnected error response
389     Transition(MCE_DISCONNECTED_STATE);
390     mceInstanceStm_.TansTargetState(MCE_INSTANCE_STATE_DISCONNECTED);
391     mceInstanceStm_.masClient_->ProcessDisConnectFinish();
392     // send disconnect to divece mgr
393     utility::Message outMsg(MSG_MCEDEVICE_INSCLIENT_DISCONNECTED);
394     mceInstanceStm_.mceDeviceMgr_.PostMessage(outMsg);
395 }
396 
DispatchProcTransportFailed(const utility::Message & msg)397 void MapMceInstanceStm::MapMceConnectingState::DispatchProcTransportFailed(const utility::Message &msg)
398 {
399     LOG_ERROR("%{public}s enter", __PRETTY_FUNCTION__);
400     if (msg.arg1_ == CONNECT_COLLISION) {  // transport's micro define
401         // retry connecting for l2cap error
402         mceInstanceStm_.masClient_->ReconnectAfterTransportFailed();
403     } else {
404         Transition(MCE_DISCONNECTED_STATE);
405         mceInstanceStm_.TansTargetState(MCE_INSTANCE_STATE_DISCONNECTED);
406         mceInstanceStm_.masClient_->ProcessDisConnectFinish();
407         // send transport error to divece mgr
408         utility::Message outMsg(MSG_MCEDEVICE_INSCLIENT_TRANSPORT_ERRO);
409         mceInstanceStm_.mceDeviceMgr_.PostMessage(outMsg);
410     }
411 }
412 
DispatchProcObexConnectedFailed()413 void MapMceInstanceStm::MapMceConnectingState::DispatchProcObexConnectedFailed()
414 {
415     LOG_ERROR("%{public}s enter", __PRETTY_FUNCTION__);
416     // receive the obex connected failed response
417     mceInstanceStm_.TansTargetState(MCE_INSTANCE_STATE_DISCONNECTED);
418     int ret = mceInstanceStm_.masClient_->ObexConnectFailedDisConnect();
419     if (ret == BT_NO_ERROR) {
420         Transition(MCE_DISCONNECTING_STATE);
421     } else {
422         Transition(MCE_DISCONNECTED_STATE);
423         mceInstanceStm_.masClient_->ProcessDisConnectFinish();
424         utility::Message outMsg(MSG_MCEDEVICE_INSCLIENT_CONNECT_FAILED);
425         mceInstanceStm_.mceDeviceMgr_.PostMessage(outMsg);
426         LOG_ERROR("%{public}s obex disconnect error", __PRETTY_FUNCTION__);
427     }
428 }
429 
DispatchProcConnectedFailed()430 void MapMceInstanceStm::MapMceConnectingState::DispatchProcConnectedFailed()
431 {
432     LOG_ERROR("%{public}s enter", __PRETTY_FUNCTION__);
433     // receive the obex connected failed response
434     mceInstanceStm_.TansTargetState(MCE_INSTANCE_STATE_DISCONNECTED);
435     Transition(MCE_DISCONNECTED_STATE);
436     mceInstanceStm_.masClient_->ProcessDisConnectFinish();
437     // send connect failed to divece mgr
438     utility::Message outMsg(MSG_MCEDEVICE_INSCLIENT_CONNECT_FAILED);
439     mceInstanceStm_.mceDeviceMgr_.PostMessage(outMsg);
440 }
441 
Dispatch(const utility::Message & msg)442 bool MapMceInstanceStm::MapMceConnectingState::Dispatch(const utility::Message &msg)
443 {
444     LOG_INFO("%{public}s enter, msg=0x%x", __PRETTY_FUNCTION__, msg.what_);
445     bool ret = true;
446     int btRet = BT_NO_ERROR;
447     switch (msg.what_) {
448         case MSG_MASSTM_OBEX_CONNECTED:
449             DispatchProcObexConnected();
450             break;
451         case MSG_MASSTM_OBEX_DISCONNECTED:  // error process
452             DispatchProcObexDisconnected();
453             break;
454         case MSG_MASSTM_OBEX_TRANSPORT_FAILED:
455             DispatchProcTransportFailed(msg);
456             break;
457         case MSG_MASSTM_OBEX_CONNECTED_FAILED:
458             DispatchProcObexConnectedFailed();
459             break;
460         case MSG_MASSTM_REQ_CONNECT:
461             // set target mod connect
462             mceInstanceStm_.TansTargetState(MCE_INSTANCE_STATE_CONNECTED);
463             break;
464         case MSG_MASSTM_REQ_DISCONNECT:
465             // set target mod disconnect
466             mceInstanceStm_.TansTargetState(MCE_INSTANCE_STATE_DISCONNECTED);
467             break;
468         case MSG_MASSTM_GAP_REQUEST_FINISH:
469             btRet = mceInstanceStm_.masClient_->ExcuteObexConnect();
470             if (btRet != BT_NO_ERROR) {
471                 DispatchProcConnectedFailed();
472                 LOG_ERROR("%{public}s executeObexConnect error after GAP", __PRETTY_FUNCTION__);
473             }
474             break;
475         case MSG_MASSTM_GAP_REQUEST_FAILED:
476             DispatchProcConnectedFailed();
477             break;
478         default:
479             ret = false;
480     }
481     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
482     return ret;
483 }
484 
485 // MCE_CONNECTED_STATE
Entry()486 void MapMceInstanceStm::MapMceConnectedState::Entry()
487 {
488     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
489 
490     utility::Message outMsg(MSG_MCEDEVICE_INSCLIENT_CONNECTED);
491     mceInstanceStm_.mceDeviceMgr_.PostMessage(outMsg);
492     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
493 }
494 
495 /**
496  * @brief exit
497  */
Exit()498 void MapMceInstanceStm::MapMceConnectedState::Exit()
499 {
500     LOG_INFO("%{public}s execute", __PRETTY_FUNCTION__);
501 }
502 
503 /**
504  * @brief dispatch the state msg
505  * @param  msg              message to process
506  * @return true     this msg has been process ok
507  * @return false    do not process , the parant can be process again
508  */
Dispatch(const utility::Message & msg)509 bool MapMceInstanceStm::MapMceConnectedState::Dispatch(const utility::Message &msg)
510 {
511     LOG_INFO("%{public}s enter, msg=0x%x", __PRETTY_FUNCTION__, msg.what_);
512     bool ret = true;
513     utility::Message outMsg(MSG_MCEDEVICE_INSCLIENT_DISCONNECTED);
514     switch (msg.what_) {
515         case MSG_MASSTM_REQ_CONNECT:
516             mceInstanceStm_.TansTargetState(MCE_INSTANCE_STATE_CONNECTED);
517             break;
518         case MSG_MASSTM_REQ_DISCONNECT:
519             // if sub state is not running, start disconnect
520             mceInstanceStm_.TansTargetState(MCE_INSTANCE_STATE_DISCONNECTED);
521             // start disconnect sequence
522             Transition(MCE_DISCONNECTING_STATE);
523             break;
524         case MSG_MASSTM_OBEX_DISCONNECTED:
525             // clear state and var, then trans to disconncted
526             Transition(MCE_DISCONNECTED_STATE);
527             mceInstanceStm_.TansTargetState(MCE_INSTANCE_STATE_DISCONNECTED);
528             mceInstanceStm_.masClient_->ProcessDisConnectFinish();
529             // send to device mgr
530             outMsg.what_ = MSG_MCEDEVICE_INSCLIENT_DISCONNECTED;
531             mceInstanceStm_.mceDeviceMgr_.PostMessage(outMsg);
532             break;
533         case MSG_MASSTM_OBEX_TRANSPORT_FAILED:
534             Transition(MCE_DISCONNECTED_STATE);
535             mceInstanceStm_.TansTargetState(MCE_INSTANCE_STATE_DISCONNECTED);
536             mceInstanceStm_.masClient_->ProcessDisConnectFinish();
537             // send to device mgr
538             outMsg.what_ = MSG_MCEDEVICE_INSCLIENT_TRANSPORT_ERRO;
539             mceInstanceStm_.mceDeviceMgr_.PostMessage(outMsg);
540             break;
541         case MSG_MASSTM_REQ_SEND_REQUEST:       // msg from service api
542         case MSG_MASSTM_REQ_SEND_REQUEST_SELF:  // msg from instance client
543             // process sending reqest
544             if (mceInstanceStm_.stmRequestPtr_ != nullptr) {
545                 int sendRet = mceInstanceStm_.masClient_->ClientSendRequest(mceInstanceStm_.stmRequestPtr_);
546                 if (sendRet == BT_NO_ERROR) {
547                     // trans to request sending
548                     Transition(MCE_CONNECTED_STATE_S_REQSENDING);
549                 } else {
550                     LOG_ERROR("%{public}s obex send request error", __PRETTY_FUNCTION__);
551                 }
552             } else {
553                 LOG_ERROR("%{public}s stmRequestPtr_ null", __PRETTY_FUNCTION__);
554             }
555             break;
556         default:
557             ret = false;
558             break;
559     }
560     return ret;
561 }
562 
563 // MCE_CONNECTED_STATE_S_REQSENDING
Entry()564 void MapMceInstanceStm::MapMceConnectedStateSubReqSending::Entry()
565 {
566     LOG_INFO("%{public}s execute", __PRETTY_FUNCTION__);
567     mceInstanceStm_.ChangeStmBusyStatus(true);
568 }
569 
Exit()570 void MapMceInstanceStm::MapMceConnectedStateSubReqSending::Exit()
571 {
572     LOG_INFO("%{public}s execute", __PRETTY_FUNCTION__);
573     mceInstanceStm_.ChangeStmBusyStatus(false);
574 }
575 
ProcReceiveRequestComplite(const utility::Message & msg)576 void MapMceInstanceStm::MapMceConnectedStateSubReqSending::ProcReceiveRequestComplite(const utility::Message &msg)
577 {
578     LOG_INFO("%{public}s execute", __PRETTY_FUNCTION__);
579     if (mceInstanceStm_.stmObexHeader_ == nullptr) {
580         LOG_ERROR("%{public}s stmObexHeader_ NULL", __PRETTY_FUNCTION__);
581         return;
582     }
583     // process response
584     int responseRet = mceInstanceStm_.masClient_->ClientProcResponseCommonProcess(*mceInstanceStm_.stmObexHeader_);
585     if ((responseRet == MCE_RESPONSE_FINISH) || (responseRet == MCE_RESPONSE_FINISH_NG) ||
586         (responseRet == MCE_RESPONSE_FINISH_NO_CALLBACK)) {  // current request process finished
587         // check target state
588         if (mceInstanceStm_.GetTargetState() == MCE_INSTANCE_STATE_DISCONNECTED) {
589             // release memory
590             mceInstanceStm_.masClient_->ClientDeleteAllSavedRequest();
591             // start disconnecting
592             Transition(MCE_DISCONNECTING_STATE);
593         } else {
594             mceInstanceStm_.masClient_->ClientSendSavedRequest();
595             // no another request execute, then trans to idle connected state
596             if (mceInstanceStm_.masClient_->GetCurrentRequest() == MCE_REQUEST_TYPE_IDLE) {
597                 Transition(MCE_CONNECTED_STATE);
598             }
599         }
600     } else {
601         // last request continue execute
602     }
603 }
604 
Dispatch(const utility::Message & msg)605 bool MapMceInstanceStm::MapMceConnectedStateSubReqSending::Dispatch(const utility::Message &msg)
606 {
607     LOG_INFO("%{public}s enter, msg=0x%x", __PRETTY_FUNCTION__, msg.what_);
608     bool ret = true;
609     switch (msg.what_) {
610         case MSG_MASSTM_RECEIVE_REQUEST_COMPLETED:
611             ProcReceiveRequestComplite(msg);
612             break;
613         case MSG_MASSTM_REQ_CONNECT:
614             // set target state
615             mceInstanceStm_.TansTargetState(MCE_INSTANCE_STATE_CONNECTED);
616             break;
617         case MSG_MASSTM_REQ_DISCONNECT:
618             // set target state
619             mceInstanceStm_.TansTargetState(MCE_INSTANCE_STATE_DISCONNECTED);
620             break;
621         case MSG_MASSTM_REQ_SEND_REQUEST:
622         case MSG_MASSTM_REQ_SEND_REQUEST_SELF:
623             // when sending , another send request coming
624             // added the request to the sending que
625             if (mceInstanceStm_.stmRequestPtr_ != nullptr) {
626                 mceInstanceStm_.masClient_->ClientSaveRequest(mceInstanceStm_.stmRequestPtr_);
627             } else {
628                 LOG_ERROR("%{public}s stmRequestPtr_ null", __PRETTY_FUNCTION__);
629             }
630             break;
631         default:
632             ret = false;
633             LOG_INFO("%{public}s ret false, msg=0x%x", __PRETTY_FUNCTION__, msg.what_);
634             break;
635     }
636     LOG_INFO("%{public}s end,ret=%{public}d", __PRETTY_FUNCTION__, ret);
637     return ret;
638 }
639 }  // namespace bluetooth
640 }  // namespace OHOS
641