1 /* 2 * Copyright (C) 2012 Samsung Electronics Co., LTD 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include <stdlib.h> 18 19 #include "tlc_communication.h" 20 21 #define LOG_TAG "tlc_communication" 22 #include "log.h" 23 24 mcResult_t tlc_open(mc_comm_ctx *comm_ctx) { 25 mcResult_t mcRet; 26 27 LOG_I("open() called"); 28 do { 29 // ------------------------------------------------------------- 30 // Step 1: Open the MobiCore device 31 LOG_I("Opening MobiCore device"); 32 mcRet = mcOpenDevice(comm_ctx->device_id); 33 if (MC_DRV_OK != mcRet) 34 LOG_I("mcOpenDevice result: %d", mcRet); 35 36 37 // ------------------------------------------------------------- 38 // Step 2: Allocate WSM buffer for the TCI 39 LOG_I("Allocating WSM for TCI"); 40 mcRet = mcMallocWsm(comm_ctx->device_id, 0, sizeof(tciMessage_t), (uint8_t **)&(comm_ctx->tci_msg), 0); 41 if (MC_DRV_OK != mcRet) { 42 LOG_E("Allocation of TCI WSM failed: %d", mcRet); 43 break; 44 } 45 46 // ------------------------------------------------------------- 47 // Step 3: Open session with the Trustlet 48 LOG_I("Opening the session"); 49 bzero(&(comm_ctx->handle), sizeof(mcSessionHandle_t)); // Clear the session handle 50 51 comm_ctx->handle.deviceId = comm_ctx->device_id; // The device ID (default device is used) 52 53 mcRet = mcOpenSession(&(comm_ctx->handle), &(comm_ctx->uuid), (uint8_t *)(comm_ctx->tci_msg), 54 (uint32_t) sizeof(tciMessage_t)); 55 if (MC_DRV_OK != mcRet) { 56 LOG_E("Open session failed: %d", mcRet); 57 break; 58 } 59 60 LOG_I("tlc_open() succeeded"); 61 } while (false); 62 63 return mcRet; 64 } 65 66 mcResult_t tlc_close(mc_comm_ctx *comm_ctx) { 67 mcResult_t mcRet; 68 69 LOG_I("close() called"); 70 do { 71 72 // ------------------------------------------------------------- 73 // Step 1: Free WSM 74 LOG_I("Free WSM"); 75 mcRet = mcFreeWsm((comm_ctx->device_id), (uint8_t *)(comm_ctx->tci_msg)); 76 if (MC_DRV_OK != mcRet) { 77 LOG_E("Free WSM failed: %d", mcRet); 78 break; 79 } 80 81 // ------------------------------------------------------------- 82 // Step 2: Close session with the Trustlet 83 LOG_I("Closing the session"); 84 mcRet = mcCloseSession(&(comm_ctx->handle)); 85 if (MC_DRV_OK != mcRet) { 86 LOG_E("Closing session failed: %d", mcRet); 87 break; 88 } 89 90 // ------------------------------------------------------------- 91 // Step 3: Close the MobiCore device 92 LOG_I("Closing MobiCore device"); 93 mcRet = mcCloseDevice(comm_ctx->device_id); 94 if (MC_DRV_OK != mcRet) { 95 LOG_E("Closing MobiCore device failed: %d", mcRet); 96 break; 97 } 98 99 LOG_I("tlc_close() succeeded"); 100 } while (false); 101 102 return mcRet; 103 } 104 105 mcResult_t tlc_communicate(mc_comm_ctx *comm_ctx) { 106 mcResult_t mcRet; 107 108 do { 109 // ------------------------------------------------------------- 110 // Step 1: signal the Trustlet 111 mcRet = mcNotify(&(comm_ctx->handle)); 112 if (MC_DRV_OK != mcRet) { 113 LOG_E("Notify failed: %d", mcRet); 114 break; 115 } 116 LOG_I("mcNotify is completed\n"); 117 118 // ------------------------------------------------------------- 119 // Step 2: Wait for the Trustlet response 120 mcRet = mcWaitNotification(&(comm_ctx->handle), -1); 121 if (MC_DRV_OK != mcRet) { 122 LOG_E("Wait for response notification failed: %d", mcRet); 123 break; 124 } 125 126 LOG_I("mcWaitNotification is completed"); 127 128 } while (false); 129 130 return mcRet; 131 } 132