Mistake on this page?
Report an issue in GitHub or email us
TestUSBMSD.h
1 /*
2  * Copyright (c) 2019, Arm Limited and affiliates.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #ifndef Test_USBMSD_H
18 #define Test_USBMSD_H
19 
20 #include "USBMSD.h"
21 
22 
23 #define USB_DEV_SN_LEN (32) // 32 hex digit UUID
24 #define USB_DEV_SN_DESC_SIZE (USB_DEV_SN_LEN * 2 + 2)
25 
26 /**
27  * Convert a C style ASCII to a USB string descriptor
28  *
29  * @param usb_desc output buffer for the USB string descriptor
30  * @param str ASCII string
31  * @param n size of usb_desc buffer, even number
32  * @returns number of bytes returned in usb_desc or -1 on failure
33  */
34 int ascii2usb_string_desc(uint8_t *usb_desc, const char *str, size_t n)
35 {
36  if (str == NULL || usb_desc == NULL || n < 4) {
37  return -1;
38  }
39  if (n % 2 != 0) {
40  return -1;
41  }
42  size_t s, d;
43  // set bString (@ offset 2 onwards) as a UNICODE UTF-16LE string
44  memset(usb_desc, 0, n);
45  for (s = 0, d = 2; str[s] != '\0' && d < n; s++, d += 2) {
46  usb_desc[d] = str[s];
47  }
48  // set bLength @ offset 0
49  usb_desc[0] = d;
50  // set bDescriptorType @ offset 1
51  usb_desc[1] = STRING_DESCRIPTOR;
52  return d;
53 }
54 
55 class TestUSBMSD: public USBMSD {
56 public:
57  TestUSBMSD(BlockDevice *bd, bool connect_blocking = true, uint16_t vendor_id = 0x0703, uint16_t product_id = 0x0104,
58  uint16_t product_release = 0x0001)
59  : USBMSD(bd, connect_blocking, vendor_id, product_id, product_release)
60  {
61 
62  }
63 
64  virtual ~TestUSBMSD()
65  {
66 
67  }
68 
69  uint32_t get_read_counter()
70  {
71  return read_counter;
72  }
73 
74  uint32_t get_program_counter()
75  {
76  return program_counter;
77  }
78 
79  void reset_counters()
80  {
81  read_counter = program_counter = erase_counter = 0;
82  }
83 
84  static void setup_serial_number()
85  {
86  char _key[128] = { 0 };
87  char _value[128] = { 0 };
88 
89  greentea_send_kv("get_serial_number", 0);
90  greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
91  TEST_ASSERT_EQUAL_STRING("serial_number", _key);
92  usb_dev_sn[USB_DEV_SN_LEN] = '\0';
93  memcpy(usb_dev_sn, _value, USB_DEV_SN_LEN);
94  ascii2usb_string_desc(_serial_num_descriptor, usb_dev_sn, USB_DEV_SN_DESC_SIZE);
95  }
96 
97  virtual const uint8_t *string_iserial_desc()
98  {
99  return (const uint8_t *)_serial_num_descriptor;
100  }
101 
102  static volatile uint32_t read_counter;
103  static volatile uint32_t program_counter;
104  static volatile uint32_t erase_counter;
105 
106 protected:
107  virtual int disk_read(uint8_t *data, uint64_t block, uint8_t count)
108  {
109  read_counter++;
110  return USBMSD::disk_read(data, block, count);
111  }
112 
113  virtual int disk_write(const uint8_t *data, uint64_t block, uint8_t count)
114  {
115  erase_counter++;
116  program_counter++;
117 
118  return USBMSD::disk_write(data, block, count);
119  }
120 private:
121  static uint8_t _serial_num_descriptor[USB_DEV_SN_DESC_SIZE];
122  static char usb_dev_sn[USB_DEV_SN_LEN + 1];
123 };
124 
125 uint8_t TestUSBMSD::_serial_num_descriptor[USB_DEV_SN_DESC_SIZE] = { 0 };
126 char TestUSBMSD::usb_dev_sn[USB_DEV_SN_LEN + 1] = { 0 };
127 
128 
129 volatile uint32_t TestUSBMSD::read_counter = 0;
130 volatile uint32_t TestUSBMSD::program_counter = 0;
131 volatile uint32_t TestUSBMSD::erase_counter = 0;
132 
133 #endif // Test_USBMSD_H
A hardware device capable of writing and reading blocks.
Definition: BlockDevice.h:47
USBMSD class: generic class in order to use all kinds of blocks storage chip.
Definition: USBMSD.h:58
USBMSD(BlockDevice *bd, bool connect_blocking=true, uint16_t vendor_id=0x0703, uint16_t product_id=0x0104, uint16_t product_release=0x0001)
Constructor.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.