summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortakeshi_hoshina <takeshi_hoshina@mail.toyota.co.jp>2020-10-22 15:16:11 +0900
committertakeshi_hoshina <takeshi_hoshina@mail.toyota.co.jp>2020-10-22 15:16:11 +0900
commiteef6be1c42037b9ec0ff8473c98ef72381362999 (patch)
treeaac4d7c6ecde6597364f4e68b0285a0249a66e0a
parent706ad73eb02caf8532deaf5d38995bd258725cb8 (diff)
-rw-r--r--LICENSE177
-rw-r--r--Makefile66
-rw-r--r--hal_api/usb_hal.h379
-rw-r--r--inc/usb_hal_debug.h31
-rw-r--r--inc/usb_hal_internal.h165
-rw-r--r--inc/usb_hal_usbhallog.h77
-rw-r--r--src/usb_hal.cpp500
7 files changed, 1395 insertions, 0 deletions
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 00000000..f433b1a5
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,177 @@
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
diff --git a/Makefile b/Makefile
new file mode 100644
index 00000000..1a107ceb
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,66 @@
+#
+# @copyright Copyright (c) 2017-2020 TOYOTA MOTOR CORPORATION.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+######### installed program/file ################
+TARGET := libusb_hal.so
+
+HEADER := ./hal_api/usb_hal.h
+
+######### include paths/files ###################
+CXXFLAGS += -I./inc/
+CXXFLAGS += -I./hal_api/
+CXXFLAGS += -I$(SDKTARGETSYSROOT)/usr/agl/include
+CXXFLAGS += -include $(SDKTARGETSYSROOT)/usr/agl/include/agl_types_obsoluted.h
+
+######### compile options #######################
+CXXFLAGS += -Wall -fPIC
+
+######### link options ##########################
+LDFLAGS += -shared
+LDFLAGS += -Wl,--no-as-needed
+LDFLAGS += -Wl,--no-undefined
+LDFLAGS += -L$(SDKTARGETSYSROOT)/usr/agl/lib
+
+RPATH :=
+
+######### linked library ########################
+LIBS += -Wl,-Bdynamic -lstdc++
+LIBS += -Wl,-Bdynamic -lNS_FrameworkUnified
+
+######### source files ##########################
+VPATH := src
+OBJS := usb_hal.o
+
+######### make targets ##########################
+all: $(TARGET)
+ @echo $@ done.
+
+$(TARGET): $(OBJS)
+ $(CC) $(LDFLAGS) -Wl,-rpath=$(RPATH) $(OBJS) $(LIBS) -o $(TARGET)
+ @echo $@ done.
+
+######### make cleans ###########################
+GCNO_FILES := $(filter %.gcno,$(OBJS:%.o=%.gcno))
+
+clean:
+ rm -f $(OBJS) $(TARGET) $(GCNO_FILES)
+ @echo $@ done.
+
+######### make installs #########################
+install:
+ install -d -m 0755 $(DESTDIR)/usr/lib
+ install -m 0755 $(TARGET) $(DESTDIR)/usr/lib
+ @echo $@ done.
diff --git a/hal_api/usb_hal.h b/hal_api/usb_hal.h
new file mode 100644
index 00000000..9f93d9b3
--- /dev/null
+++ b/hal_api/usb_hal.h
@@ -0,0 +1,379 @@
+/*
+ * @copyright Copyright (c) 2017-2020 TOYOTA MOTOR CORPORATION.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file usb_hal.h
+ * @brief usb_hal API
+ */
+
+#ifndef HAL_API_USB_HAL_H_
+#define HAL_API_USB_HAL_H_
+
+#include "native_service/frameworkunified_types.h"
+
+/** @addtogroup device_detection_service
+ * @{
+ */
+/** @addtogroup usb_hal
+ * @ingroup device_detection_service
+ * @{
+ */
+
+/**
+ * \~english Definition of Host/Function mode of USB OTG port.
+ */
+enum UsbRoleType {
+ /**
+ * \~english Host mode
+ */
+ USB_ROLE_HOST = 0,
+ /**
+ * \~english Function mode
+ */
+ USB_ROLE_FUNCTION,
+};
+
+/**
+ * \ingroup GetUsbRoleSwitch
+ * \~english @par Brief
+ * Get USB role
+ * \~english @param [out] usb_role
+ * UsbRoleType* - USB role
+ * \~english @retval eFrameworkunifiedStatusOK OK
+ * \~english @retval eFrameworkunifiedStatusNullPointer NULL Pointer
+ * \~english @retval eFrameworkunifiedStatusFail Abnormal Error
+ * \~english @par Prerequisite
+ * - When State value is "status faile", Can not get host/funtcion mode of USB OTG port.
+ * \~english @par Change of internal state
+ * - None.
+ * \~english @par Conditions of processing failure
+ * - Input parameter error
+ * - eFrameworkunifiedStatusNullPointer
+ * - Internal process error(open/read)
+ * - eFrameworkunifiedStatusFail
+ * - Internal process error(State value is "status faile")
+ * - eFrameworkunifiedStatusFail
+ * \~english @par Classification
+ * Public
+ * \~english @par Type
+ * Sync
+ * \~english @par Detail
+ * - Get Host/Funtcion mode of USB OTG port.
+ * - The API can not be used by multiple processes, threads at the same time.
+ * \~english @see SetUsbRoleSwitch
+ */
+EFrameworkunifiedStatus GetUsbRoleSwitch(UsbRoleType* usb_role);
+
+/**
+ * \ingroup SetUsbRoleSwitch
+ * \~english @par Brief
+ * Set USB role.
+ * \~english @param [in] usb_role
+ * UsbRoleType - USB role
+ * \~english @retval eFrameworkunifiedStatusOK OK
+ * \~english @retval eFrameworkunifiedStatusInvldParam Invalid Parameter
+ * \~english @retval eFrameworkunifiedStatusFail Abnormal Error
+ * \~english @par Prerequisite
+ * - When State value is "status faile", Can not set host/funtcion mode of USB OTG port.
+ * \~english @par Change of internal state
+ * - Change "USB role is host mode"or"USB role is host mode"
+ * \~english @par Conditions of processing failure
+ * - Input parameter error
+ * - eFrameworkunifiedStatusInvldParam
+ * - Internal process error(open/read/write)
+ * - eFrameworkunifiedStatusFail
+ * - Internal process error(State value is "status faile")
+ * - eFrameworkunifiedStatusFail
+ * \~english @par Classification
+ * Public
+ * \~english @par Type
+ * Sync
+ * \~english @par Detail
+ * - Set Host/Funtcion mode of USB OTG port.
+ * - The API can not be used by multiple processes, threads at the same time.
+ * \~english @see GetUsbRoleSwitch
+ */
+EFrameworkunifiedStatus SetUsbRoleSwitch(UsbRoleType usb_role);
+
+/**
+ * \~english USB authentication error information
+ */
+enum UsbAuthenticationError {
+ /**
+ * \~english USB authentication no error
+ */
+ USB_AUTHENTICATION_ERROR_NONE = 0,
+ /**
+ * \~english The connected device has no response
+ */
+ USB_AUTHENTICATION_ERROR_ENUMERATE,
+ /**
+ * \~english The device connected to bus-powered HUB is overcurrent
+ */
+ USB_AUTHENTICATION_ERROR_HUB_OVERCURRENT,
+ /**
+ * \~english HUB tier levels
+ */
+ USB_AUTHENTICATION_ERROR_HUB_TIER_LEVELS,
+ /**
+ * \~english The number of connected devices is more than supported
+ */
+ USB_AUTHENTICATION_ERROR_CONNECTION_EXCESS
+};
+
+/**
+ * \ingroup CheckUsbAuthenticationError
+ * \~english @par Brief
+ * Check USB authentication error
+ * \~english @param [out] usb_auth_error
+ * UsbAuthenticationError* - USB authentication error
+ * \~english @retval eFrameworkunifiedStatusOK OK
+ * \~english @retval eFrameworkunifiedStatusNullPointer NULL Pointer
+ * \~english @retval eFrameworkunifiedStatusFail Abnormal Error
+ * \~english @par Prerequisite
+ * - When State value is "status faile", Can not check USB authentication error .
+ * \~english @par Change of internal state
+ * - None.
+ * \~english @par Conditions of processing failure
+ * - Input parameter error
+ * - eFrameworkunifiedStatusNullPointer
+ * - Internal process error(open/read)
+ * - eFrameworkunifiedStatusFail
+ * - Internal process error(State value is "status faile")
+ * - eFrameworkunifiedStatusFail
+ * \~english @par Classification
+ * Public
+ * \~english @par Type
+ * Sync
+ * \~english @par Detail
+ * - Check USB authentication error information.
+ * - The API can not be used by multiple processes, threads at the same time.
+ * \~english @see None.
+ */
+EFrameworkunifiedStatus CheckUsbAuthenticationError(UsbAuthenticationError* usb_auth_error);
+
+/**
+ * \~english Definition of USB port number.
+ * - The USB port number is defined according to the RefHW,
+ * modification will be necessary if there's any change about USB port.
+ */
+enum UsbPortNumber {
+ /**
+ * \~english USB0(IF-BOX)
+ */
+ USB_PORT_NUMBER_0 = 0,
+ /**
+ * \~english USB1(DCM)
+ */
+ USB_PORT_NUMBER_1,
+ /**
+ * \~english USB2(DSRC/ETC)
+ */
+ USB_PORT_NUMBER_2,
+ /**
+ * \~english USB3(External deck)
+ */
+ USB_PORT_NUMBER_3,
+ /**
+ * \~english USB4(Drive recorder)
+ */
+ USB_PORT_NUMBER_4,
+ /**
+ * \~english Maximum of USB port number.
+ */
+ USB_PORT_NUMBER_MAX
+};
+
+/**
+ * \ingroup ResetUsbVbus
+ * \~english @par Brief
+ * USB VBus Reset.
+ * \~english @param [in] usb_port_no
+ * UsbPortNumber - USB port number
+ * \~english @retval eFrameworkunifiedStatusOK OK
+ * \~english @retval eFrameworkunifiedStatusInvldParam Invalid Parameter
+ * \~english @retval eFrameworkunifiedStatusFail Abnormal Error
+ * \~english @par Prerequisite
+ * - When State value is "status faile", Can not USB VBus reset .
+ * \~english @par Change of internal state
+ * - "USB power ON" from the "USB power OFF".
+ * \~english @par Conditions of processing failure
+ * - Input parameter error
+ * - eFrameworkunifiedStatusInvldParam
+ * - Internal process error(open/read/write)
+ * - eFrameworkunifiedStatusFail
+ * - Internal process error(State value is "status faile")
+ * - eFrameworkunifiedStatusFail
+ * - Not change internal state.
+ * \~english @par Classification
+ * Public
+ * \~english @par Type
+ * Sync
+ * \~english @par Detail
+ * - USB VBus Reset.
+ * - The API is a block I/F which costs a while due to the sleep time between power off and power on.
+ * - The API can not be used by multiple processes, threads at the same time.
+ * \~english @see IsUsbPortPowered, PowerOnUsbPort, PowerOffUsbPort
+ */
+EFrameworkunifiedStatus ResetUsbVbus(UsbPortNumber usb_port_no);
+
+/**
+ * \ingroup IsUsbPortPowered
+ * \~english @par Brief
+ * Check the power state of USB port.
+ * \~english @param [in] usb_port_no
+ * UsbPortNumber - USB port number
+ * \~english @param [out] is_powered
+ * bool*\n
+ * - true : Power is on
+ * - false : Power is off
+ * \~english @retval eFrameworkunifiedStatusOK OK
+ * \~english @retval eFrameworkunifiedStatusInvldParam Invalid Parameter
+ * \~english @retval eFrameworkunifiedStatusNullPointer NULL Pointer
+ * \~english @retval eFrameworkunifiedStatusFail Abnormal Error
+ * \~english @par Prerequisite
+ * - None.
+ * \~english @par Change of internal state
+ * - None.
+ * \~english @par Conditions of processing failure
+ * - Input parameter error
+ * - eFrameworkunifiedStatusInvldParam
+ * - eFrameworkunifiedStatusNullPointer
+ * - Internal process error(open/read)
+ * - eFrameworkunifiedStatusFail
+ * \~english @par Classification
+ * Public
+ * \~english @par Type
+ * Sync
+ * \~english @par Detail
+ * - Check the power state of USB port.
+ * - The API can not be used by multiple processes, threads at the same time.
+ * \~english @see ResetUsbVbus, PowerOnUsbPort, PowerOffUsbPort
+ */
+EFrameworkunifiedStatus IsUsbPortPowered(UsbPortNumber usb_port_no, bool* is_powered);
+
+/**
+ * \ingroup PowerOnUsbPort
+ * \~english @par Brief
+ * Power on USB port.
+ * \~english @param [in] usb_port_no
+ * UsbPortNumber - USB port number
+ * \~english @retval eFrameworkunifiedStatusOK OK
+ * \~english @retval eFrameworkunifiedStatusInvldParam Invalid Parameter
+ * \~english @retval eFrameworkunifiedStatusFail Abnormal Error
+ * \~english @par Prerequisite
+ * - When State value is "status faile", Can not power on USB port.
+ * \~english @par Change of internal state
+ * - Change state "USB power on"
+ * \~english @par Conditions of processing failure
+ * - Input parameter error
+ * - eFrameworkunifiedStatusInvldParam
+ * - Internal process error(open/read/write)
+ * - eFrameworkunifiedStatusFail
+ * - Internal process error(State value is "status faile")
+ * - eFrameworkunifiedStatusFail
+ * - Not change internal state.
+ * \~english @par Classification
+ * Public
+ * \~english @par Type
+ * Sync
+ * \~english @par Detail
+ * - Power on USB port.
+ * - The API can not be used by multiple processes, threads at the same time.
+ * \~english @see ResetUsbVbus, IsUsbPortPowered, PowerOffUsbPort
+ */
+EFrameworkunifiedStatus PowerOnUsbPort(UsbPortNumber usb_port_no);
+
+/**
+ * \ingroup PowerOffUsbPort
+ * \~english @par Brief
+ * Power off USB port.
+ * \~english @param [in] usb_port_no
+ * UsbPortNumber - USB port number
+ * \~english @retval eFrameworkunifiedStatusOK OK
+ * \~english @retval eFrameworkunifiedStatusInvldParam Invalid Parameter
+ * \~english @retval eFrameworkunifiedStatusFail Abnormal Error
+ * \~english @par Prerequisite
+ * - None.
+ * \~english @par Change of internal state
+ * - Change state "USB power off"
+ * \~english @par Conditions of processing failure
+ * - Input parameter error
+ * - eFrameworkunifiedStatusInvldParam
+ * - Internal process error(open/read/write)
+ * - eFrameworkunifiedStatusFail
+ * \~english @par Classification
+ * Public
+ * \~english @par Type
+ * Sync
+ * \~english @par Detail
+ * - Power off USB port.
+ * - The API can not be used by multiple processes, threads at the same time.
+ * \~english @see ResetUsbVbus, IsUsbPortPowered, PowerOnUsbPort
+ */
+EFrameworkunifiedStatus PowerOffUsbPort(UsbPortNumber usb_port_no);
+
+/**
+ * \~english USB abnormal status
+ */
+enum UsbAbnormalStatus {
+ /**
+ * \~english USB is not abnormal
+ */
+ USB_ABNORMAL_STATUS_NONE = 0,
+ /**
+ * \~english USB is overcurrent
+ */
+ USB_ABNORMAL_STATUS_OVERCURRENT
+};
+
+/**
+ * \ingroup CheckUsbAbnormalStatus
+ * \~english @par Brief
+ * Check the abnormal status of USB port
+ * \~english @param [in] usb_port_no
+ * UsbPortNumber - USB port number
+ * \~english @param [out] usb_abnormal_status
+ * UsbAbnormalStatus* - USB abnormal status
+ * \~english @retval eFrameworkunifiedStatusOK OK
+ * \~english @retval eFrameworkunifiedStatusInvldParam Invalid Parameter
+ * \~english @retval eFrameworkunifiedStatusNullPointer NULL Pointer
+ * \~english @retval eFrameworkunifiedStatusFail Abnormal Error
+ * \~english @par Prerequisite
+ * - None.
+ * \~english @par Change of internal state
+ * - change state "usb port status faile" or "usb port status ok".
+ * \~english @par Conditions of processing failure
+ * - Input parameter error
+ * - eFrameworkunifiedStatusInvldParam
+ * - eFrameworkunifiedStatusNullPointer
+ * - Internal process error(open/read)
+ * - eFrameworkunifiedStatusFail
+ * \~english @par Classification
+ * Public
+ * \~english @par Type
+ * Sync
+ * \~english @par Detail
+ * - Check the abnormal status of USB port, including overcurrent and so on.
+ * - The API can not be used by multiple processes, threads at the same time.
+ * \~english @see None.
+ */
+EFrameworkunifiedStatus CheckUsbAbnormalStatus(UsbPortNumber usb_port_no, UsbAbnormalStatus* usb_abnormal_status);
+
+/** @}*/ // end of usb_hal
+/** @}*/ // end of device_detection_service
+
+#endif // HAL_API_USB_HAL_H_
diff --git a/inc/usb_hal_debug.h b/inc/usb_hal_debug.h
new file mode 100644
index 00000000..15b307a3
--- /dev/null
+++ b/inc/usb_hal_debug.h
@@ -0,0 +1,31 @@
+/*
+ * @copyright Copyright (c) 2017-2020 TOYOTA MOTOR CORPORATION.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef INC_USB_HAL_DEBUG_H_
+#define INC_USB_HAL_DEBUG_H_
+
+#include "usb_hal_usbhallog.h"
+
+/* Log output control flag */
+#define USB_LOG_ENABLE_ERROR /* Error log */
+
+#ifdef USB_LOG_ENABLE_ERROR
+#define USB_ERROR_LOG(fmt, ...) FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, fmt, ##__VA_ARGS__)
+#else
+#define USB_ERROR_LOG(fmt, ...)
+#endif
+
+#endif // INC_USB_HAL_DEBUG_H_
diff --git a/inc/usb_hal_internal.h b/inc/usb_hal_internal.h
new file mode 100644
index 00000000..86c39056
--- /dev/null
+++ b/inc/usb_hal_internal.h
@@ -0,0 +1,165 @@
+/*
+ * @copyright Copyright (c) 2017-2020 TOYOTA MOTOR CORPORATION.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef INC_USB_HAL_INTERNAL_H_
+#define INC_USB_HAL_INTERNAL_H_
+
+#define FILE_CONTENT_LENGTH (32)
+#define FILE_PATH_LENGTH (128)
+
+/**
+ * USB role file
+ */
+#define USB_ROLE_FILE "/sys/devices/platform/soc/ee080200.usb-phy/role"
+
+/**
+ * USB host value
+ */
+#define USB_HOST_STRING "host\n"
+
+/**
+ * USB function value
+ */
+#define USB_FUNCTION_STRING "peripheral\n"
+
+/**
+ * gpio high value
+ */
+const char kUsbGpioHighValue = '1';
+
+/**
+ * gpio low value
+ */
+const char kUsbGpioLowValue = '0';
+
+/**
+ * gpio export file
+ */
+const char kUsbGpioExportFile[] = "/sys/class/gpio/export";
+
+/**
+ * gpio file prefix
+ */
+const char kUsbGpioFilePrefix[] = "/sys/class/gpio/gpio";
+
+/**
+ * gpio direction file
+ */
+const char kUsbGpioDirectionFile[] = "/direction";
+
+/**
+ * gpio direction in
+ */
+const char kUsbGpioDirectionIn[] = "in";
+
+/**
+ * gpio direction out high
+ */
+const char kUsbGpioDirectionHigh[] = "high";
+
+/**
+ * gpio direction out low
+ */
+const char kUsbGpioDirectionLow[] = "low";
+
+/**
+ * gpio value file
+ */
+const char kUsbGpioValueFile[] = "/value";
+
+/**
+ * structure of USB role switch information
+ */
+struct UsbRoleSwitchInfo {
+ const char* file_path; //!< USB role switch file path
+ const char* usb_host_value; //!< USB host value
+ const char* usb_function_value; //!< USB function value
+};
+
+/**
+ * USB gpio port definition
+ */
+enum UsbGpioPort {
+ USB_GPIO_PORT_USB0_PWEN = 0, //!< USB0 power enable
+ USB_GPIO_PORT_USB1_PWEN, //!< USB1 power enable
+ USB_GPIO_PORT_USB2_PWEN, //!< USB2 power enable
+ USB_GPIO_PORT_USB0_OVC, //!< USB0 overcurrent
+ USB_GPIO_PORT_USB1_OVC, //!< USB1 overcurrent
+ USB_GPIO_PORT_USB2_OVC, //!< USB2 overcurrent
+ USB_GPIO_PORT_MAX
+};
+
+/**
+ * USB gpio port value definition
+ */
+enum GpioPortValue {
+ GPIO_PORT_OFF = 0, //!< gpio port on
+ GPIO_PORT_ON //!< gpio port off
+};
+
+/**
+ * Length for USB gpio port name
+ */
+#define USB_PORT_NAME_LENGTH (8)
+
+/**
+ * USB gpio port information
+ */
+struct UsbGpioPortInfo {
+ bool active_low; //!< active low or high
+ bool is_output; //!< direction output or input
+ const char* port_name; //!< gpio port name
+ char default_value; //!< output port default value (high or low)
+};
+
+/**
+ * USB gpio port config table
+ */
+const char kUsbGpioPortNameUsb0Pwen[] = "375";
+const char kUsbGpioPortNameUsb1Pwen[] = "387";
+const char kUsbGpioPortNameUsb2Pwen[] = "389";
+const char kUsbGpioPortNameUsb0Ovc[] = "376";
+const char kUsbGpioPortNameUsb1Ovc[] = "388";
+const char kUsbGpioPortNameUsb2Ovc[] = "390";
+
+const UsbGpioPortInfo kUsbGpioInfo[] = {
+ { false, true, kUsbGpioPortNameUsb0Pwen, kUsbGpioHighValue },
+ { false, true, kUsbGpioPortNameUsb1Pwen, kUsbGpioHighValue },
+ { false, true, kUsbGpioPortNameUsb2Pwen, kUsbGpioHighValue },
+ { false, false, kUsbGpioPortNameUsb0Ovc, kUsbGpioLowValue },
+ { false, false, kUsbGpioPortNameUsb1Ovc, kUsbGpioLowValue },
+ { false, false, kUsbGpioPortNameUsb2Ovc, kUsbGpioLowValue }
+};
+
+/**
+ * gpio port list of USB power enable
+ */
+const UsbGpioPort kUsbPowerEnableGpio[] = {
+ USB_GPIO_PORT_USB0_PWEN,
+ USB_GPIO_PORT_USB1_PWEN,
+ USB_GPIO_PORT_USB2_PWEN
+};
+
+/**
+ * gpio port list of USB overcurrent
+ */
+const UsbGpioPort kUsbOvercurrentGpio[] = {
+ USB_GPIO_PORT_USB0_OVC,
+ USB_GPIO_PORT_USB1_OVC,
+ USB_GPIO_PORT_USB2_OVC
+};
+
+#endif // INC_USB_HAL_INTERNAL_H_
diff --git a/inc/usb_hal_usbhallog.h b/inc/usb_hal_usbhallog.h
new file mode 100644
index 00000000..53c0a6b3
--- /dev/null
+++ b/inc/usb_hal_usbhallog.h
@@ -0,0 +1,77 @@
+/*
+ * @copyright Copyright (c) 2017-2020 TOYOTA MOTOR CORPORATION.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef INC_USB_HAL_USBHALLOG_H_
+#define INC_USB_HAL_USBHALLOG_H_
+
+#include <native_service/ns_logger_if.h>
+
+#define ZONE_INIT ZONEMASK(10)
+#define ZONE_FUNC ZONEMASK(11)
+#define ZONE_MEM ZONEMASK(12)
+#define ZONE_13 ZONEMASK(13)
+#define ZONE_14 ZONEMASK(14)
+#define ZONE_15 ZONEMASK(15)
+#define ZONE_16 ZONEMASK(16)
+#define ZONE_17 ZONEMASK(17)
+#define ZONE_18 ZONEMASK(18)
+#define ZONE_19 ZONEMASK(19)
+#define ZONE_20 ZONEMASK(20)
+#define ZONE_21 ZONEMASK(21)
+#define ZONE_22 ZONEMASK(22)
+#define ZONE_23 ZONEMASK(23)
+#define ZONE_24 ZONEMASK(24)
+#define ZONE_25 ZONEMASK(25)
+#define ZONE_26 ZONEMASK(26)
+#define ZONE_27 ZONEMASK(27)
+#define ZONE_28 ZONEMASK(28)
+#define ZONE_INFO ZONEMASK(29)
+#define ZONE_WARN ZONEMASK(30)
+#define ZONE_ERR ZONEMASK(31)
+
+#define ZONE_TEXT_10 "Init"
+#define ZONE_TEXT_11 "Function"
+#define ZONE_TEXT_12 "Memory"
+#define ZONE_TEXT_13 ""
+#define ZONE_TEXT_14 ""
+#define ZONE_TEXT_15 ""
+#define ZONE_TEXT_16 ""
+#define ZONE_TEXT_17 ""
+#define ZONE_TEXT_18 ""
+#define ZONE_TEXT_19 ""
+#define ZONE_TEXT_20 ""
+#define ZONE_TEXT_21 ""
+#define ZONE_TEXT_22 ""
+#define ZONE_TEXT_23 ""
+#define ZONE_TEXT_24 ""
+#define ZONE_TEXT_25 ""
+#define ZONE_TEXT_26 ""
+#define ZONE_TEXT_27 ""
+#define ZONE_TEXT_28 ""
+#define ZONE_TEXT_29 "Info"
+#define ZONE_TEXT_30 "Warning"
+#define ZONE_TEXT_31 "Error"
+
+#ifndef FRAMEWORKUNIFIEDLOGOPTIONS
+#define FRAMEWORKUNIFIEDLOGOPTIONS (LSHAREDMEM) // LPRINT , LMSGQ, LSLOGGER
+#endif
+
+#ifndef FRAMEWORKUNIFIEDLOGAPPZONES
+#define FRAMEWORKUNIFIEDLOGAPPZONES ZONE_ERR, ZONE_WARN, ZONE_INFO
+#endif
+
+extern const CHAR AppName[]; // NOLINT (defind outside)
+#endif // INC_USB_HAL_USBHALLOG_H_
diff --git a/src/usb_hal.cpp b/src/usb_hal.cpp
new file mode 100644
index 00000000..7e09ad41
--- /dev/null
+++ b/src/usb_hal.cpp
@@ -0,0 +1,500 @@
+/*
+ * @copyright Copyright (c) 2017-2020 TOYOTA MOTOR CORPORATION.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * Information.
+ * This source code is a sample source code .
+ * Implementation of the function must be performed by the vendor.
+ */
+
+#include "usb_hal.h"
+#include "usb_hal_internal.h"
+#include "usb_hal_debug.h"
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdio.h>
+
+/**
+ * USB role switch information table
+ */
+const UsbRoleSwitchInfo kUsbRoleSwitchTable = {USB_ROLE_FILE, USB_HOST_STRING, USB_FUNCTION_STRING};
+
+/**
+ * Read content of USB role switch file, then determine HOST/FUNCTION role
+ * according to the specified HOST/FUNCTION value.
+ */
+static EFrameworkunifiedStatus GetUsbRoleType(const char* file_path, const char* usb_host_value, const char* usb_function_value,
+ UsbRoleType* usb_role) {
+ if (!file_path || !usb_host_value || !usb_function_value || !usb_role) {
+ USB_ERROR_LOG("parameter is NULL.");
+ return eFrameworkunifiedStatusFail;
+ }
+
+ // 1. open role switch file
+ int fd = open(file_path, O_RDONLY);
+ if (-1 == fd) {
+ USB_ERROR_LOG("open [%s] failed.(%d, %s)", file_path, errno, strerror(errno));
+ return eFrameworkunifiedStatusFail;
+ }
+
+ // 2. read file content
+ char file_content[FILE_CONTENT_LENGTH];
+ memset(file_content, 0, sizeof(file_content));
+ ssize_t ret = read(fd, file_content, sizeof(file_content) - 1);
+ int read_error = errno;
+
+ // 3. close file
+ close(fd);
+
+ if (-1 == ret) {
+ USB_ERROR_LOG("read failed.(%d, %s)", read_error, strerror(read_error));
+ return eFrameworkunifiedStatusFail;
+ }
+
+ // 4. compare file content to HOST/FUNCTION value
+ if (!strcmp(usb_host_value, file_content)) {
+ *usb_role = USB_ROLE_HOST;
+ } else if (!strcmp(usb_function_value, file_content)) {
+ *usb_role = USB_ROLE_FUNCTION;
+ } else {
+ USB_ERROR_LOG("content error.[%s]", file_content);
+ return eFrameworkunifiedStatusFail;
+ }
+
+ return eFrameworkunifiedStatusOK;
+}
+
+/**
+ * Write the specified role value to USB role switch file.
+ */
+static EFrameworkunifiedStatus SetUsbRoleValue(const char* file_path, const char* role_value) {
+ if (!file_path || !role_value) {
+ USB_ERROR_LOG("parameter is NULL.");
+ return eFrameworkunifiedStatusFail;
+ }
+
+ // 1. open role switch file
+ int fd = open(file_path, O_WRONLY | O_TRUNC);
+ if (-1 == fd) {
+ USB_ERROR_LOG("open [%s] failed.(%d, %s)", file_path, errno, strerror(errno));
+ return eFrameworkunifiedStatusFail;
+ }
+
+ // 2. write role file
+ ssize_t ret = write(fd, role_value, strlen(role_value));
+ int write_error = errno;
+
+ // 3. close file
+ close(fd);
+
+ // 4. check write result
+ if (static_cast<ssize_t>(strlen(role_value)) != ret) {
+ USB_ERROR_LOG("write [%s] failed, ret=%zd.(%d, %s)", role_value, ret, write_error, strerror(write_error));
+ return eFrameworkunifiedStatusFail;
+ } else {
+ return eFrameworkunifiedStatusOK;
+ }
+}
+
+/**
+ * Check whether the specified USB port supports power control.
+ */
+static bool CheckSupportPowerControl(UsbPortNumber usb_port_no) {
+ if (USB_PORT_NUMBER_MAX <= usb_port_no) {
+ return false;
+ }
+
+ return true;
+}
+
+/**
+ * Check whether the specified gpio port is available.
+ */
+static bool CheckGpioPortAvailable(UsbGpioPort gpio_port) {
+ if (USB_GPIO_PORT_MAX <= gpio_port) {
+ USB_ERROR_LOG("port %d is invalid.", gpio_port);
+ return false;
+ }
+
+ if (!kUsbGpioInfo[gpio_port].port_name || (0 == strlen(kUsbGpioInfo[gpio_port].port_name))) {
+ USB_ERROR_LOG("port %d is not available.", gpio_port);
+ return false;
+ }
+
+ return true;
+}
+
+/**
+ * Open gpio export file.
+ */
+static int OpenExportFile() {
+ return open(kUsbGpioExportFile, O_WRONLY);
+}
+
+/**
+ * Open the direction file of the specified gpio port.
+ */
+static int OpenDirectionFile(const char* port_name) {
+ if (!port_name || (0 == strlen(port_name))) {
+ USB_ERROR_LOG("port name is invalid.");
+ return -1;
+ }
+
+ char dir_file[FILE_PATH_LENGTH];
+ // /sys/class/gpio/gpio123/direction
+ snprintf(dir_file, sizeof(dir_file), "%s%s%s", kUsbGpioFilePrefix, port_name, kUsbGpioDirectionFile);
+ return open(dir_file, O_RDWR);
+}
+
+/**
+ * Open the value file of the specified gpio port.
+ */
+static int OpenValueFile(const char* port_name, bool ro) {
+ if (!port_name || (0 == strlen(port_name))) {
+ USB_ERROR_LOG("port name is invalid.");
+ return -1;
+ }
+
+ char value_file[FILE_PATH_LENGTH];
+ // /sys/class/gpio/gpio123/value
+ snprintf(value_file, sizeof(value_file), "%s%s%s", kUsbGpioFilePrefix, port_name, kUsbGpioValueFile);
+ return open(value_file, ro ? O_RDONLY : O_RDWR);
+}
+
+/**
+ * Initialize gpio port value.
+ */
+static EFrameworkunifiedStatus InitializeGpioPort(UsbGpioPort gpio_port) {
+ if (!CheckGpioPortAvailable(gpio_port)) {
+ return eFrameworkunifiedStatusFail;
+ }
+
+ char port_file[FILE_PATH_LENGTH];
+ snprintf(port_file, sizeof(port_file), "%s%s", kUsbGpioFilePrefix, kUsbGpioInfo[gpio_port].port_name);
+
+ // check if port is exported already
+ if (0 == access(port_file, F_OK)) {
+ return eFrameworkunifiedStatusOK;
+ }
+
+ // open the export file
+ int export_fd = OpenExportFile();
+ if (-1 == export_fd) {
+ USB_ERROR_LOG("open [%s] failed.(%d, %s)", "export", errno, strerror(errno));
+ return eFrameworkunifiedStatusFail;
+ }
+
+ // do export by writing the export file
+ ssize_t ret = write(export_fd, kUsbGpioInfo[gpio_port].port_name, strlen(kUsbGpioInfo[gpio_port].port_name));
+ int write_error = errno;
+
+ // close the export file
+ close(export_fd);
+
+ // check write result
+ if (static_cast<ssize_t>(strlen(kUsbGpioInfo[gpio_port].port_name)) != ret) {
+ USB_ERROR_LOG("write [%s] failed, ret=%zd.(%d, %s)", kUsbGpioInfo[gpio_port].port_name, ret, write_error,
+ strerror(write_error));
+ return eFrameworkunifiedStatusFail;
+ }
+
+ // open the direction file
+ int direction_fd = OpenDirectionFile(kUsbGpioInfo[gpio_port].port_name);
+ if (-1 == direction_fd) {
+ USB_ERROR_LOG("open direction file failed.(%d, %s)", errno, strerror(errno));
+ return eFrameworkunifiedStatusFail;
+ }
+
+ // set direction
+ const char* direction_value;
+ if (!kUsbGpioInfo[gpio_port].is_output) {
+ // for input port, set direction as "in"
+ direction_value = kUsbGpioDirectionIn;
+ } else {
+ if (kUsbGpioHighValue == kUsbGpioInfo[gpio_port].default_value) {
+ // for output port, if default value is high, set direction as "high"
+ direction_value = kUsbGpioDirectionHigh;
+ } else if (kUsbGpioLowValue == kUsbGpioInfo[gpio_port].default_value) {
+ // for output port, if default value is low, set direction as "low"
+ direction_value = kUsbGpioDirectionLow;
+ } else {
+ USB_ERROR_LOG("unknown default value[%c]", kUsbGpioInfo[gpio_port].default_value);
+ return eFrameworkunifiedStatusFail;
+ }
+ }
+
+ ret = write(direction_fd, direction_value, strlen(direction_value));
+ write_error = errno;
+ close(direction_fd);
+ if (static_cast<ssize_t>(strlen(direction_value)) != ret) {
+ USB_ERROR_LOG("write direction %s failed, ret=%zd.(%d, %s)", direction_value, ret, write_error,
+ strerror(write_error));
+ return eFrameworkunifiedStatusFail;
+ }
+
+ return eFrameworkunifiedStatusOK;
+}
+
+/**
+ * Get gpio port value.
+ */
+static EFrameworkunifiedStatus GetGpioPortValue(UsbGpioPort port_name, bool* port_value) {
+ if ((USB_GPIO_PORT_MAX <= port_name) || !port_value) {
+ USB_ERROR_LOG("parameter is invalid.");
+ return eFrameworkunifiedStatusFail;
+ }
+
+ // initialize gpio port
+ EFrameworkunifiedStatus result = InitializeGpioPort(port_name);
+ if (eFrameworkunifiedStatusOK != result) {
+ return result;
+ }
+
+ // open port value file
+ int fd = OpenValueFile(kUsbGpioInfo[port_name].port_name, true);
+ if (-1 == fd) {
+ USB_ERROR_LOG("open value file failed.(%d, %s)", errno, strerror(errno));
+ return eFrameworkunifiedStatusFail;
+ }
+
+ // read file content
+ char file_content = '\0';
+ ssize_t ret = read(fd, &file_content, sizeof(file_content));
+ int read_error = errno;
+
+ // close file
+ close(fd);
+
+ if (sizeof(file_content) != ret) {
+ USB_ERROR_LOG("read failed.(%d, %s)", read_error, strerror(read_error));
+ return eFrameworkunifiedStatusFail;
+ }
+
+ // compare file content to high/low value
+ if (kUsbGpioHighValue == file_content) {
+ *port_value = (kUsbGpioInfo[port_name].active_low ? false : true);
+ } else if (kUsbGpioLowValue == file_content) {
+ *port_value = (kUsbGpioInfo[port_name].active_low ? true : false);
+ } else {
+ USB_ERROR_LOG("content error.[%c]", file_content);
+ return eFrameworkunifiedStatusFail;
+ }
+
+ return eFrameworkunifiedStatusOK;
+}
+
+/**
+ * Set gpio port value.
+ */
+static EFrameworkunifiedStatus SetGpioPortValue(UsbGpioPort port_name, GpioPortValue port_value) {
+ if (USB_GPIO_PORT_MAX <= port_name) {
+ USB_ERROR_LOG("parameter is invalid.");
+ return eFrameworkunifiedStatusFail;
+ }
+
+ // initialize gpio port
+ EFrameworkunifiedStatus result = InitializeGpioPort(port_name);
+ if (eFrameworkunifiedStatusOK != result) {
+ return result;
+ }
+
+ // check direction is output
+ if (!kUsbGpioInfo[port_name].is_output) {
+ USB_ERROR_LOG("%d not an output port.", port_name);
+ return eFrameworkunifiedStatusFail;
+ }
+
+ // open port value file
+ int fd = OpenValueFile(kUsbGpioInfo[port_name].port_name, false);
+ if (-1 == fd) {
+ USB_ERROR_LOG("open value file failed.(%d, %s)", errno, strerror(errno));
+ return eFrameworkunifiedStatusFail;
+ }
+
+ // write value
+ bool port_active = (GPIO_PORT_ON == port_value ? true : false);
+ bool write_low = (kUsbGpioInfo[port_name].active_low ? port_active : !port_active);
+ char write_value = (write_low ? kUsbGpioLowValue : kUsbGpioHighValue);
+ ssize_t ret = write(fd, &write_value, sizeof(write_value));
+ int write_error = errno;
+
+ // close file
+ close(fd);
+
+ // check write result
+ if (sizeof(write_value) != ret) {
+ USB_ERROR_LOG("write [%c] failed, ret=%zd.(%d, %s)", write_value, ret, write_error, strerror(write_error));
+ return eFrameworkunifiedStatusFail;
+ } else {
+ return eFrameworkunifiedStatusOK;
+ }
+}
+
+/**
+ * Check overcurrent.
+ */
+static EFrameworkunifiedStatus CheckUsbOvercurrent(UsbPortNumber usb_port_no, bool* is_ovc) {
+ if ((USB_PORT_NUMBER_MAX <= usb_port_no) || !is_ovc) {
+ return eFrameworkunifiedStatusFail;
+ }
+
+ return GetGpioPortValue(kUsbOvercurrentGpio[usb_port_no], is_ovc);
+}
+
+EFrameworkunifiedStatus GetUsbRoleSwitch(UsbRoleType* usb_role) {
+ // check NULL pointer
+ if (!usb_role) {
+ USB_ERROR_LOG("parameter is NULL.");
+ return eFrameworkunifiedStatusNullPointer;
+ }
+
+ return GetUsbRoleType(kUsbRoleSwitchTable.file_path, kUsbRoleSwitchTable.usb_host_value,
+ kUsbRoleSwitchTable.usb_function_value, usb_role);
+}
+
+EFrameworkunifiedStatus SetUsbRoleSwitch(UsbRoleType usb_role) {
+ if ((USB_ROLE_HOST != usb_role) && (USB_ROLE_FUNCTION != usb_role)) {
+ USB_ERROR_LOG("parameter error.[%d]", usb_role);
+ return eFrameworkunifiedStatusInvldParam;
+ }
+
+ // check current usb role:
+ // allow to switch only when usb_role is different from current usb role.
+ UsbRoleType current_role;
+ if (eFrameworkunifiedStatusOK != GetUsbRoleSwitch(&current_role)) {
+ return eFrameworkunifiedStatusFail;
+ }
+
+ if (current_role == usb_role) {
+ return eFrameworkunifiedStatusOK;
+ }
+
+ // check usb role type
+ // - HOST: write HOST value to role file
+ // - FUNCTION: write FUNCTION value to role file
+ return SetUsbRoleValue(kUsbRoleSwitchTable.file_path, (USB_ROLE_HOST == usb_role)
+ ? kUsbRoleSwitchTable.usb_host_value
+ : kUsbRoleSwitchTable.usb_function_value);
+}
+
+EFrameworkunifiedStatus CheckUsbAuthenticationError(UsbAuthenticationError* usb_auth_error) {
+ // check NULL pointer
+ if (!usb_auth_error) {
+ USB_ERROR_LOG("parameter is NULL.");
+ return eFrameworkunifiedStatusNullPointer;
+ }
+
+ // Empty Stub implementations
+ *usb_auth_error = USB_AUTHENTICATION_ERROR_NONE;
+ return eFrameworkunifiedStatusOK;
+}
+
+EFrameworkunifiedStatus ResetUsbVbus(UsbPortNumber usb_port_no) {
+ // check support power control
+ if (!CheckSupportPowerControl(usb_port_no)) {
+ return eFrameworkunifiedStatusInvldParam;
+ }
+
+ // step 1: power off USB port
+ EFrameworkunifiedStatus result = SetGpioPortValue(kUsbPowerEnableGpio[usb_port_no], GPIO_PORT_OFF);
+ if (eFrameworkunifiedStatusOK != result) {
+ USB_ERROR_LOG("power off USB%d failed.", usb_port_no);
+ return result;
+ }
+
+ // step 2: sleep 1.1 sec
+ usleep(1100 * 1000);
+
+ // step 3: power on USB port
+ result = SetGpioPortValue(kUsbPowerEnableGpio[usb_port_no], GPIO_PORT_ON);
+ if (eFrameworkunifiedStatusOK != result) {
+ USB_ERROR_LOG("power on USB%d failed.", usb_port_no);
+ }
+ return result;
+}
+
+EFrameworkunifiedStatus IsUsbPortPowered(UsbPortNumber usb_port_no, bool* is_powered) {
+ if (!is_powered) {
+ return eFrameworkunifiedStatusNullPointer;
+ }
+
+ // check support power control
+ if (!CheckSupportPowerControl(usb_port_no)) {
+ return eFrameworkunifiedStatusInvldParam;
+ }
+
+ return GetGpioPortValue(kUsbPowerEnableGpio[usb_port_no], is_powered);
+}
+
+EFrameworkunifiedStatus PowerOnUsbPort(UsbPortNumber usb_port_no) {
+ // check support power control
+ if (!CheckSupportPowerControl(usb_port_no)) {
+ return eFrameworkunifiedStatusInvldParam;
+ }
+ bool current_power = false;
+ if (eFrameworkunifiedStatusOK != GetGpioPortValue(kUsbPowerEnableGpio[usb_port_no], &current_power)) {
+ return eFrameworkunifiedStatusFail;
+ }
+
+ if (current_power) {
+ return eFrameworkunifiedStatusOK;
+ }
+ return SetGpioPortValue(kUsbPowerEnableGpio[usb_port_no], GPIO_PORT_ON);
+}
+
+EFrameworkunifiedStatus PowerOffUsbPort(UsbPortNumber usb_port_no) {
+ // check support power control
+ if (!CheckSupportPowerControl(usb_port_no)) {
+ return eFrameworkunifiedStatusInvldParam;
+ }
+ bool current_power = false;
+ if (eFrameworkunifiedStatusOK != GetGpioPortValue(kUsbPowerEnableGpio[usb_port_no], &current_power)) {
+ return eFrameworkunifiedStatusFail;
+ }
+
+ if (!current_power) {
+ return eFrameworkunifiedStatusOK;
+ }
+
+ return SetGpioPortValue(kUsbPowerEnableGpio[usb_port_no], GPIO_PORT_OFF);
+}
+
+EFrameworkunifiedStatus CheckUsbAbnormalStatus(UsbPortNumber usb_port_no, UsbAbnormalStatus* usb_abnormal_status) {
+ if (USB_PORT_NUMBER_MAX <= usb_port_no) {
+ return eFrameworkunifiedStatusInvldParam;
+ }
+
+ // check NULL pointer
+ if (!usb_abnormal_status) {
+ return eFrameworkunifiedStatusNullPointer;
+ }
+
+ // check overcurrent
+ bool is_ovc = false;
+ EFrameworkunifiedStatus result = CheckUsbOvercurrent(usb_port_no, &is_ovc);
+ if (eFrameworkunifiedStatusOK == result) {
+ if (is_ovc) {
+ *usb_abnormal_status = USB_ABNORMAL_STATUS_OVERCURRENT;
+ } else {
+ *usb_abnormal_status = USB_ABNORMAL_STATUS_NONE;
+ }
+ }
+ return result;
+}