1 /* 2 * Copyright (C) 2023 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 #ifndef MMS_NETWORK_CLIENT_H 17 #define MMS_NETWORK_CLIENT_H 18 19 #include <curl/curl.h> 20 #include <curl/easy.h> 21 22 #include <cinttypes> 23 #include <cmath> 24 #include <memory> 25 #include <string> 26 27 namespace OHOS { 28 namespace Telephony { 29 enum class HttpReqType { 30 HTTP_REQUEST_TYPE_GET, 31 HTTP_REQUEST_TYPE_POST, 32 }; 33 class CURLClean { 34 public: 35 void operator()(CURL *p) const; 36 }; 37 38 class MmsNetworkClient { 39 public: 40 explicit MmsNetworkClient(int32_t slotId); 41 virtual ~MmsNetworkClient(); 42 int32_t Execute(const std::string &method, const std::string &mmsc, const std::string &data); 43 void InitLibCurl(); 44 void DestoryLibCurl(); 45 46 private: 47 void SetIfaceName(const std::string &ifaceName); 48 std::string GetIfaceName(); 49 int32_t HttpPost(const std::string &strUrl, const std::string &strData, std::string &strResponse); 50 int32_t HttpGet(const std::string &strUrl, std::string &strResponse); 51 int32_t HttpRequestExec( 52 HttpReqType type, const std::string &strUrl, const std::string &strData, std::string &strResponse); 53 int32_t ParseExecResult(const std::unique_ptr<CURL, CURLClean> &mmsCurl, int32_t result); 54 int32_t PostUrl(const std::string &mmsc, const std::string &filename); 55 int32_t GetUrl(const std::string &mmsc, const std::string &storeDirName); 56 int32_t SetCurlOpt(const std::unique_ptr<CURL, CURLClean> &mmsCurl, HttpReqType type, const std::string &strUrl, 57 const std::string &strData, std::string &strResponse); 58 int32_t SetCurlOptCommon(const std::unique_ptr<CURL, CURLClean> &mmsCurl, const std::string &strUrl); 59 static int32_t DataCallback(const std::string &data, size_t size, size_t nmemb, std::string *strBuffer); 60 bool WriteBufferToFile(const std::unique_ptr<char[]> &buff, uint32_t len, const std::string &strPathName) const; 61 void DeleteMmsPdu(const std::string &dbUrl); 62 bool GetMmsPduFromFile(const std::string &fileName, std::string &strBuf); 63 bool GetMmsPduFromDataBase(const std::string &dbUrl, std::string &strBuf); 64 65 private: 66 static constexpr int32_t DEFAULT_ERROR_SIZE = 256; 67 static constexpr int32_t MAX_MMSC_SIZE = 50; 68 static constexpr int32_t MAX_MMSC_PROXY_SIZE = 50; 69 char errorBuffer_[DEFAULT_ERROR_SIZE]; 70 char mmscChar_[MAX_MMSC_SIZE]; 71 char proxyChar_[MAX_MMSC_PROXY_SIZE]; 72 int64_t connectionTimeout_; 73 int64_t transOpTimeout_; 74 int64_t lastTransTime_ = 0; 75 std::string ifaceName_; 76 int32_t slotId_ = -1; 77 struct curl_slist *mmsHttpHeaderlist_ = nullptr; 78 }; 79 } // namespace Telephony 80 } // namespace OHOS 81 #endif